import random
guesses = 3
number = random.randint(1, 10)
print (number) #<- To test what number im supposed to write.
while guesses > 0: # When you have more than 0 guesses -> guess another number
guess = input("Guess: ")
if guess == number : # If guess is equal to number -> end game
print ('Your guess is right!.')
break
if guess != number : # If not number -> -1 guess
print ("Nope!")
guesses -= 1
if guesses == 0: #If all 3 guesses are made -> end game
print("That was it! Sorry.")
print(number,"was the right answer!")
我做错了什么? 想不通,希望大家帮忙^-^
如果您能教我如何改进我的编程,请随时写信告诉我如何去做!我愿意学习新东西顺便说一句,对不起我的英语不好 :3(编辑:当我猜对了数字时,它仍然说“不!”,我不得不猜另一个数字。)
最佳答案
这看起来像 Python3。如果是这样,请改用 guess = int(input("Guess: "))
。
在 Python3 中 input()
返回一个字符串,您正在将该字符串与一个永远无法工作的整数进行比较。因此,将 input()
的返回值转换为一个整数,以确保您在比较苹果与苹果。
关于python - 为什么这段代码不起作用(我是编程新手,python),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35368448/