0

好的,我是 python 2.7.5 的新手,我的代码似乎不起作用。它只是一个基本的随机数猜测“游戏”

from random import randint
number = randint(1,100)
play = input("Hello! Would you like to play (Y/N) ")
if play in('y','Y'):
    print("I've chosen a number between 1 and 100.")
    guess = int(input("what is my number?")
    while(guess != number):
        if(guess > number):
            print("Too High!")
        else:
            print("Too Low!")
        guess = int(input("Please guess again: "))
    print("Correct! You guessed my number!")
if play in('n','N'):
    print('Stop wasting my time then!')

我收到一个突出显示的错误,并说它是“无效的语法”,我们将很高兴为您提供帮助

4

2 回答 2

1

您在这一行缺少括号:

guess = int(input("what is my number?")

您打开了两个,但只关闭了一个。

解决方案是关闭两者:

guess = int(input("what is my number?"))
于 2013-10-10T20:05:11.653 回答
0

缺少括号是问题所在。但是你应该使用 raw_input() 而不是 input()。

from random import randint
number = randint(1,100)
play = raw_input("Hello! Would you like to play (Y/N) ")
if play in('y','Y'):
    print("I've chosen a number between 1 and 100.")
    guess = int(raw_input("what is my number?"))
        while(guess != number):
            if(guess > number):
                print("Too High!")
            else:
                print("Too Low!")
            guess = int(raw_input("Please guess again: "))
        print("Correct! You guessed my number!")
if play in('n','N'):
    print('Stop wasting my time then!')
于 2013-10-10T20:22:01.500 回答