-1

我花了 1.5 个小时的大部分时间在这件事上工作,但我就是无法让它工作。在它开始抛出这些奇怪的语法错误之前,它会继续说“猜低,再试一次”。我遇到的这些错误甚至没有在 shell 中运行。

这是代码:

import random
gend = random.randint(1,100) #make random number
def compare(gend,guess): #start function

    guessnum = 0 #set guessnumber to zero for later printing

    if gend < guess: #compare gend number against guessed number
        int(raw_input("Too high. Try again. ")) #request another number for user
        guessnum = guessnum + 1 #add 1 to guessnumber, for later printing
    elif gend > guess: #compare gend number against guessed number
        int(raw_input("Too low. Try again. "))#request another number for user
        guessnum = guessnum + 1 #compare gend number against guessed number
    elif gend == guess: #if guessed number=gend, do this
            keepalive = 'much spook' #stop the while loop
            print "Congratulations! You got it in %d guess." % (guessnum)#print da amount of guesses

keepalive = 'rekt' #keepalive substitute

print 'Time to play a guessing game' #no exp needed

guess = int(raw_input("Enter a number between 1 and 100: ") #start off the game

while keepalive == 'rekt': #while loop that does all the work
    compare(gend,guess) #funcion werk

raw_input("Any key to exit") #keep console open so it doesnt autoclose -.-

我还要感谢你们的帮助,可以说是大多数语言的最佳学习资源。

4

2 回答 2

1

您在语法错误上方的行中缺少右括号。

guess = int(raw_input("Enter a number between 1 and 100: ") #start off the game
                                                           ^
于 2013-11-02T20:09:21.913 回答
1

语法错误是由于一行造成的。Python 只能告诉你它注意到哪里出了问题,这不一定是事情实际走下坡路的地方。中缺少一个右括号guess = int(raw_input("Enter a number between 1 and 100: ")

它一直说“太低,再试一次”是由于两个错误:

  • 当您要求另一个数字时,您不会分配给guess(您读取一个数字,解析它,然后将其丢弃),因此guess永远不要更改。
  • 在函数内部,keepalive = 'much spook'不会更改keepalive循环外使用的相同内容。有几种方法可以解决这个问题,但哪种方法合适取决于您以前的经验和您正在学习的材料 - 请查阅它以查看是否提到了类似的内容。
于 2013-11-02T20:10:39.000 回答