试着玩一些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'
首先,每次调用函数<function character_class at 0x025078B0>
后出现的character_class
是什么?这在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)
答案 0 :(得分:2)
您成功调用了执行打印的功能,然后每当您执行print character_class
时,每次还要告诉Python打印功能本身。不要那样做。
另外,我不知道为什么你在最后一个中使用了%
,在那里你从raw_input传递了数据。再说一次,不要这样做:
character_class(input1, input2, input3)
答案 1 :(得分:0)
首先,让我们看看你的输出:
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
三次,从而显示输出的前四行。
当您调用print character_class
时,您将函数定义作为参数传递给print函数。输出<function character_class at 0x025078B0>
是已定义函数的引用位置。重点是,您不需要在print
来电之前致电character_class
,因为您已经定义了您的功能来调用打印三次。当python执行character_class(20,40,60)
时,它将进入函数并执行您定义的每行代码,并使用字符串替换插入您传递的参数。不需要额外的print character_class
,因为您的功能代码会为您执行print
。
最后,您正确获取原始输入。 raw_input
函数将从控制台获取您的输入并返回已分配给变量的结果。你的电话(character_class % (input1, input2, input3)
)几乎是正确的,但是我想你正在混淆用字符串变量替换来调用一个函数。
您只需要通过传递从输入中获得的三个参数来调用您的函数:character_class(input1, input2, input3)
,您的函数将为您执行打印,因为您已将其定义为上面的操作!
答案 2 :(得分:-1)
我对unicode不太了解。我相信它是键盘上特定字符的参考代码,而不是。此外,你的问题不太清楚。但是,我写了这篇文章,希望它有些重新格式化并回答了你的几个问题:
[
{Option: 'Yes', Total: 10},
{Option: 'No', Total: 8}
]
我对编程还很陌生,所以我的代码可能有点“关闭”。或马虎或其他什么。无论如何,我将intel,str和agil分配给一个类属性,然后通过原始输入定义它们。
运行此脚本时,它将提示您输入1-3下的属性,然后通过character_class.input()显示它们。这些输入值分配给str,intel和agil。这是否以任何方式回答了您的问题?如果不让我知道。