我正在学习Python2.7.x中的函数,我正在使用的书中的一个建议是征求用户的输入以获取脚本的值。关于如何在函数中使用raw_input
的建议如下:
You need to use int() to convert what you get from raw_input()
我还不知道如何使用int()
。到目前为止,我一直在努力:
def cheeses_and_crackers(cheeses, crackers):
print "You have %d types of cheeses." % cheeses
print "You have %d types of crackers." % crackers
print "That is a lot of cheese and crackers!\n"
print "How many cheeses do you have?"
cheeses1 = raw_input("> ")
int(cheeses1)
print "How many types of crackers do you have?"
crackers1 = raw_input("> ")
int(crackers1)
cheeses_and_crackers(cheeses1, crackers1)
尝试运行此命令时,出现的错误如下:
TypeError: %d format: a number is required, not str
我在猜测如何使用int()
,因此我也希望能在基本语法方面得到一些帮助。在
int
不改变用户输入(字符串,实际上是不可变的),而是构造一个整数,然后返回它。在由于没有为返回值指定任何名称,因此返回值将丢失。在
演示:
相关问题 更多 >
编程相关推荐