我的时间刚刚破碎

时间:2013-10-10 20:03:48

标签: python-2.7

好的,所以我是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!')

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

2 个答案:

答案 0 :(得分:1)

您在此行中缺少一个括号:

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

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

解决方案是关闭两者:

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

答案 1 :(得分: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!')