我的初学者项目出错了

时间:2017-09-17 03:45:33

标签: 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 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

2 个答案:

答案 0 :(得分:5)

检查括号:

raw_input("pick an integer between %s and %s") %(low, high) #bad

raw_input("pick an integer between %s and %s" % (low, high)) #good

答案 1 :(得分:2)

您的%s替换位于括号raw_input的括号之外;他们应该立即遵循字符串(如在基普的回答中)。

只需使用.format语法即可。我认为更容易理解。

此:

"pick an integer between %s and %s" % (low, high)

可以写成:

"pick an integer between {} and {}".format(low, high)

您也可以使用Ruby样式(在Python 3.5 +中):

f"pick an integer between {low} and {high}"