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(编辑:当我猜对了正确的数字,它仍然说&#34;没有!&#34;我不得不猜猜另一个数字。)
答案 0 :(得分:2)
这看起来像Python3。如果是,请改用guess = int(input("Guess: "))
。
在Python3中input()
返回一个字符串,你将该字符串与一个永远不会有效的整数进行比较。因此,将input()
的返回值转换为整数,以确保您将苹果与苹果进行比较。
答案 1 :(得分:0)
你需要在输入前放置int,所以:
guess = int(input("Guess: "))
这会将猜测转换为整数,因此代码会识别它。
答案 2 :(得分:0)
input()
命令返回一个字符串,字符串不等于数字("3" == 3
计算结果为false
)。您可以使用int(...)
函数将字符串(或浮点数)转换为整数。
我假设您正在使用Python 3.x,因为print
是一个函数。如果您使用的是Python 2.x,则应使用raw_input()
,因为input()
会导致解释器将输入的内容视为Python代码并执行它(如eval(...)
函数所做的那样)
在99.999%的情况下,不想要执行用户输入。 ; - )
答案 3 :(得分:-1)
您的程序需要的另一个重要的事情是提示用户,以便他们知道他们将对您的程序做些什么。我已相应地添加了提示。
import random
print ("Hello. We are going to be playing a guessing game where you guess a random number from 1-10. You get three guesses.")
number = random.randint(1, 10)
# print (number) #<- To test what number im supposed to write.
guesses = 3
while guesses > 0: # When you have more than 0 guesses -> guess another number
guess = input("Enter your 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!")