Python 我的初学者项目出错了
我对编程相关的一切都是新手,刚刚完成了一门python入门课程,并试图开始一些项目。 我遇到了一些我不知道的事情Python 我的初学者项目出错了,python,Python,我对编程相关的一切都是新手,刚刚完成了一门python入门课程,并试图开始一些项目。 我遇到了一些我不知道的事情 lives=3 while lives>0: low=raw_input("what is the lower range that you will guess? Numbers only please.") high=raw_input("what is the higher range that you will guess? Numbers only plea
lives=3
while lives>0:
low=raw_input("what is the lower range that you will guess? Numbers only please.")
high=raw_input("what is the higher range that you will guess? Numbers only please")
thenumber=randint(int(low),int(high))
if int(raw_input("pick an integer between %s and %s") %(low, high))==thenumber:
print "you won!"
将两个变量都设置为“1”后,它将打印“选择一个介于%s和%s之间的整数”,而不是“选择一个介于1和1之间的整数”
编辑:在提交一个数字进行猜测之后,我还得到
TypeError: not all arguments converted during string formatting
检查括号:
raw_input("pick an integer between %s and %s") %(low, high) #bad
raw_input("pick an integer between %s and %s" % (low, high)) #good
检查括号:
raw_input("pick an integer between %s and %s") %(low, high) #bad
raw_input("pick an integer between %s and %s" % (low, high)) #good
您的
%s
替换在包围原始输入的括号外
;他们应该立即跟随字符串(如kip的回答)
只需使用.format
语法即可。在我看来,更容易理解
这:
可以写为:
"pick an integer between {} and {}".format(low, high)
您也可以用Ruby风格(在Python 3.5+中)来实现:
您的
%s
替换在包围原始输入的括号外
;他们应该立即跟随字符串(如kip的回答)
只需使用.format
语法即可。在我看来,更容易理解
这:
可以写为:
"pick an integer between {} and {}".format(low, high)
您也可以用Ruby风格(在Python 3.5+中)来实现:
一般来说,试着用谷歌搜索你遇到的任何错误:一般来说,试着用谷歌搜索你遇到的任何错误:不仅仅是在开始的时候。你未来的自我会因为你没有正确地评论一个剧本而踢你的屁股…:)不仅仅是在一开始。你未来的自我会因为你没有正确地评论一个剧本而踢你的屁股…:)