[英]Why isn't this function working?
只是尝试使用一些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中没有出现。
另外,我试图从用户处获取raw_input,以插入到函数中。 这是不可能的,还是我做错了?
修订:将最后一行代码更改为: 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)
您成功调用了执行打印的函数,然后每次您print character_class
时,也告诉Python打印函数本身 。 只是不要那样做。
另外,我不知道为什么您在上一个中使用了%
,这是您从raw_input传递数据的地方。 同样,不要那样做:
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.
<function character_class at 0x025078B0>.
然后生成此输出的代码:
character_class(20, 40, 60)
print character_class
您已经在上面定义了character_class函数以采用三个参数,并分别调用了三个打印函数。 调用character_class(20, 40, 60)
,您将传入20、40和60作为参数。 然后,您的函数将在上次调用中使用换行符来调用print
3次,从而显示输出的前四行。
调用print character_class
,您会将函数定义作为参数传递给print函数。 输出<function character_class at 0x025078B0>
是已定义函数的参考位置 。 关键是,您无需在character_class
调用之前调用print
,因为您已定义了三次调用print的函数。 当python执行character_class(20,40,60)
,它将进入函数并执行您定义的每一行代码,并插入使用字符串替换传递的参数。 不需要额外的print character_class
,因为您的功能代码会为您执行print
。
最后,您可以正确获取原始输入。 raw_input
函数将从控制台获取您的输入并返回结果,该结果已分配给变量。 您的调用( character_class % (input1, input2, input3)
)几乎是正确的,但是我认为您正在混用字符串变量替换来调用函数。
您只需通过传递从输入获得的三个参数来调用函数: character_class(input1, input2, input3)
,您的函数将为您进行打印,就像您在上面定义的那样!
我对unicode不太了解。 我相信它是键盘上特定字符的参考代码,不是。 另外,您的问题不太清楚。 但是,我写了这篇文章,希望它能重新格式化并回答您遇到的几个问题:
class character_class(object):
def __init__(self, intel, str, agil):
self.intel = intel
self.str = str
self.agil = agil
def input(self):
str = input1
intel = input2
agil = input3
print "A barbarian's main attribute starts at: %r." % str
print "A wizard's main attribute is intel, and it starts at: %r." % intel
print "An archer's main attribute is agility, and has the default agility speed of: %r.\n" % agil
cont = raw_input("")
input1 = raw_input("Barbarian str:")
input2 = raw_input("Wizard intel:")
input3 = raw_input("Archer agil:")
character_class = character_class(input1, input2, input3)
character_class.input()
我对编程还是很陌生,所以我的代码可能有点“过时”,草率或其他。 无论如何,我将intel,str和agil分配给了一个类属性,然后通过原始输入定义了这些属性。
运行此脚本时,它将提示您输入1-3下的属性,然后通过character_class.input()显示它们。 这些输入值分配给str,intel和agil。 这是否以任何方式回答了您的问题? 如果没有让我知道。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.