使用Python 3.3.2中的While循环获取语法错误

时间:2013-06-10 20:48:16

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

出于某种原因,我在“while”上获得了无效的语法。我似乎无法找出为什么因为某些原因使用了这项工作。

1 个答案:

答案 0 :(得分:0)

你在这一行的末尾错过了一个右括号:

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? ")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

guess将是一个字符串,因此您应该将其转换为整数,然后将其视为一个:

guess = int(input("What's yer guess? "))

最后,这一行缺少结束双引号:

print ("No more guesses! Better luck next time, matey! )
                                                      ^