Python新手,试图弄清楚这里出了什么问题。制作一个简单的游戏,我必须猜测计算机随机生成的数字。谢谢你的帮助。
这就是我所拥有的:
guessed == random.randint(1,100)
print("I guessed a number between 1 and 100. Try to find it!")
entered = 0
while entered != guessed
entered = raw_input("Enter your suggestion:")
entered = int(guessed_number)
if entered > guessed
print('Try less')
else
print('Try more')
print('You win!')
答案 0 :(得分:3)
你在条件和循环结束时遗漏了冒号,又名while entered != guessed:
。将它们添加到if
和else
行的末尾。此外,您在分配==
而非分配运算符(guessed
)时使用比较(=
)运算符。
你也会注意到它打印“试试更多”,即使他们猜到正确的数字,然后它会打印“你赢了!”。我会将此作为练习留给新开发人员修复。
答案 1 :(得分:3)
entered = int(guessed_number)
没有意义,因为你没有guessed_number变量。我认为你打算这样做
entered = int(raw_input("Enter your suggestion:")
此外,你在阻止开始的时候缺少冒号,如果,等等。
答案 2 :(得分:2)
欢迎使用Python 3.x!这是给你的固定代码。
#Import Random
import random as r
#Create a random Number!
guessed = r.randint(1,100)
print("I guessed a number between 1 and 100. Try to find it!")
#Initiate variable --entered--
entered = 0
while (entered != guessed):
entered = int(input("Enter your suggestion:"))
#Fixed your if/else tree with correct indents and an elif.
if (entered > guessed):
print('Try less')
elif (entered <guessed):
print('Try more')
else:
print('You win!')
答案 3 :(得分:1)
要添加到列表中:
guessed == random.randint(1,100)
应该是
guessed = random.randint(1,100)
我确定你宁愿分配给guessed
而不是比较它random.randint(1,100)
然后抛弃那个比较的结果。
答案 4 :(得分:0)
entered = int(guessed_number)
没有任何意义。 'guessed_number'没有变量。
我已编辑您的代码以使其正常工作:
import random
guessed = r.randint(1,100)
print("I guessed a number between 1 and 100. Try to find it!")
entered = 0
while (entered != guessed):
entered = int(input("Enter your suggestion:"))
if (entered > guessed):
print('Try less')
elif (entered <guessed):
print('Try more')
else:
print('You win!')
希望有所帮助!
〜爱德华