我花了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 -.-
我还要感谢你们的帮助,可以说是大多数语言的最佳学习资源。
答案 0 :(得分:1)
您在语法错误上方的行上缺少右括号。
guess = int(raw_input("Enter a number between 1 and 100: ") #start off the game
^
答案 1 :(得分:1)
语法错误是由之前的行引起的。 Python只能告诉你注意到某些东西的关闭,这不一定是事情真正走下坡路的地方。 guess = int(raw_input("Enter a number between 1 and 100: ")
中缺少一个结束副本。
它一直说“太低,再试一次”是由于两个错误:
guess
(你读了一个号码,解析它,然后扔掉它),所以guess
永远不会改变。keepalive = 'much spook'
不会更改在循环外使用的keepalive
。有几种方法可以解决这个问题,但哪一种方法是合适的取决于您之前的经验和您正在学习的材料 - 请参考它以了解是否提到了类似的内容。