Loops 在Python 3.3.2中使用While循环获取Sytax错误
这是我的密码:Loops 在Python 3.3.2中使用While循环获取Sytax错误,loops,python-3.x,while-loop,syntax-error,Loops,Python 3.x,While Loop,Syntax Error,这是我的密码: import random secret = random.randint (1, 99) guess = 0 tries = 0 print ("Ahoy! I'm the Dread Pirate Roberts, and I have a secret!") print ("It is a number from 1 to 99. I'll give you 6 tries. " while tries < 6 and guess != secret: gue
import random
secret = random.randint (1, 99)
guess = 0
tries = 0
print ("Ahoy! I'm the Dread Pirate Roberts, and I have a secret!")
print ("It is a number from 1 to 99. I'll give you 6 tries. "
while tries < 6 and guess != secret:
guess = input ("What's yer guess? ")
if guess < secret:
print ("Too low, ye scurvy dog!")
elif guess > secret:
print ("Too High, landlubber!")
tries = tries + 1
if (guess == secret):
print ("AVAST! Ye got it! Found my secret, ye did it!"
else:
print ("No more guesses! Better luck next time, matey!)
print ("The secret number was"), secret
随机导入
secret=random.randint(1,99)
猜测=0
尝试=0
打印(“喂!我是可怕的海盗罗伯茨,我有一个秘密!”)
打印(“这是一个从1到99的数字。我会给你6次尝试。”
尝试<6并猜测!=秘密:
猜测=输入(“你的猜测是什么?”)
如果猜测<秘密:
打印(“太低了,你这个坏蛋!”
elif guess>秘密:
打印(“太高了,landubber!”)
尝试=尝试+1
如果(猜测==秘密):
打印(“阿瓦斯特!你找到了!找到了我的秘密,你做到了!”
其他:
打印(“不要再猜测了!祝你下次好运,伙计!)
打印(“机密号码”),机密
由于某种原因,我在“while”上得到了一个无效的语法。由于某种原因,我似乎无法找到为什么使用该语法。您在这一行末尾缺少一个右括号:
print ("It is a number from 1 to 99. I'll give you 6 tries. "
^
此行缩进不正确:
while tries < 6 and guess != secret:
guess = input ("What's yer guess? ")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
最后,此行缺少结束双引号:
print ("No more guesses! Better luck next time, matey! )
^
好的,谢谢你成为我的第二双眼睛。我非常感谢。但是当我运行这个程序时,我得到了以下输出:>>>>===========================================================================================================================================================================================================================>>啊!我是可怕的海盗罗伯茨,我有一个秘密!这是一个从1到99的数字。我会给你6次尝试。你猜是什么?15次k(最近一次调用):文件“C:\Users\Mark Stewart\Desktop\Number.py”,第12行,if guess
guess
转换为整数。导入随机密码=random.randint(1,99)guess=0次尝试=0次打印(“喂!我是可怕的海盗罗伯茨,我有一个秘密!)print(“这是一个从1到99的数字,我给你6次尝试。”)而guess!=secret并尝试<6:guess=int(输入(“你猜什么?”))如果guessprint ("No more guesses! Better luck next time, matey! )
^