只是想玩一些LPTHW函数。我在这里编的:
def character_class(intel, str, agil):
print "A barbarian's main attribute starts at: %d." % str
print "A wizard's main attribute is intel, and it starts at: %d." % intel
print "An archer's main attribute is agility, and has the default agility speed of: %d.\n" % agil
character_class(20, 40, 60)
print character_class
character_class(20 + 40, 40 + 50, 100 + 100)
print character_class
input1 = raw_input("Barbarian str:")
input2 = raw_input("Wizard intel:")
input3 = raw_input("Archer agil:")
character_class % (input1, input2, input3)
print character_class
下面是我在Powershell中得到的结果:
A barbarian's main attribute starts at: 40.
A wizard's main attribute is intel, and it starts at: 20.
An archer's main attribute is agility, and has the default agility speed of: 60.
<function character_class at 0x025078B0>
A barbarian's main attribute starts at: 90.
A wizard's main attribute is intel, and it starts at: 60.
An archer's main attribute is agility, and has the default agility speed of: 200.
<function character_class at 0x025078B0>
Barbarian str:200
Wizard intel:300
Archer agil:400
Traceback (most recent call last):
File "test19.py", line 16, in <module>
character_class % (input1, input2, input3)
TypeError: unsupported operand type(s) for %: 'function' and 'tuple'
首先,在每次调用函数character_class
之后出现的<function character_class at 0x025078B0>
是什么?这在LPTHW练习19中没有出现。你知道吗
另外,我还尝试从用户那里获取原始输入,以插入到函数中。这是不可能的,还是我做错了?你知道吗
修订:将最后一行代码更改为:character_class(input1, input2, input3)
这就是我现在得到的错误:
A barbarian's main attribute starts at: 40.
A wizard's main attribute is intel, and it starts at: 20.
An archer's main attribute is agility, and has the default agility speed of: 60.
A barbarian's main attribute starts at: 90.
A wizard's main attribute is intel, and it starts at: 60.
An archer's main attribute is agility, and has the default agility speed of: 200.
Barbarian str:1000
Wizard intel:2000
Archer agil:3000
Traceback (most recent call last):
File "test19.py", line 16, in <module>
character_class(input1, input2, input3)
File "test19.py", line 2, in character_class
print "A barbarian's main attribute starts at: %d." % str
TypeError: %d format: a number is required, not str
最终修订:
def character_class(intel, str, agil):
print "A barbarian's main attribute starts at: %d." % str
print "A wizard's main attribute is intel, and it starts at: %d." % intel
print "An archer's main attribute is agility, and has the default agility speed of: %d.\n" % agil
character_class(20, 40, 60)
character_class(20 + 40, 40 + 50, 100 + 100)
input1 = raw_input("Barbarian str:")
input2 = raw_input("Wizard intel:")
input3 = raw_input("Archer agil:")
inputa = int(input1)
inputb = int(input2)
inputc = int(input3)
character_class(inputa, inputb, inputc)
首先,让我们看看您的输出:
然后生成这个输出的代码:
您已经在上面定义了character\u类函数来获取三个参数并分别调用print函数三次。当您调用
character_class(20, 40, 60)
时,您将传入20、40和60作为参数。然后,您的函数将调用print
三次,在最后一次调用中使用换行符,结果显示输出的前四行。你知道吗调用
print character_class
时,将函数定义作为参数传递给print函数。输出<function character_class at 0x025078B0>
,是所定义函数的参考位置。关键是,您不需要在character_class
调用之前调用print
,因为您已经定义了调用print三次的函数。当python执行character_class(20,40,60)
时,它将进入函数并执行您定义的每一行代码,并插入使用字符串替换传递的参数。附加的print character_class
不是必需的,因为函数代码为您执行print
-ing。你知道吗最后,您正确地获得了原始输入。
raw_input
函数将从控制台获取您的输入并返回结果,您已将结果赋给变量。您的调用(character_class % (input1, input2, input3)
)几乎是正确的,但是我认为您将调用函数与字符串变量替换混为一谈。你知道吗您只需通过传递从input获得的三个参数来调用函数:
character_class(input1, input2, input3)
,您的函数将为您进行打印,正如您在上面定义的那样!你知道吗我对unicode知识不太了解。我相信它是键盘上特定字符的参考代码。另外,你的问题也不太清楚。不过,我写了这篇文章,希望它能重新格式化并回答您的几个问题:
我对编程还是比较陌生的,所以我的代码可能有点“脱节”或者马虎之类的。无论如何,我将intel、str和agil分配给一个类属性,然后通过原始输入定义它们。你知道吗
当您运行此脚本时,它将提示您输入input 1-3下的属性,然后通过character显示它们_类输入(). 这些输入值被分配给str、intel和agil。这回答了你的问题吗?如果不告诉我。你知道吗
您成功地调用了执行打印的函数,然后每次还告诉Python打印函数本身,当您执行
print character_class
时。别那么做。你知道吗另外,我也不知道为什么在上一篇文章中使用了
%
,在这里您要传递来自原始输入的数据。同样,不要这样做:相关问题 更多 >
编程相关推荐