0

这是我的代码:

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”上得到了无效的语法。我似乎无法找出原因,因为它出于某种原因使用过。

4

1 回答 1

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! )
                                                      ^
于 2013-06-10T20:49:19.400 回答