IF语句只会考虑ELIF吗?

时间:2018-09-15 14:07:45

标签: python python-3.x

怎么了?我正在上学我的学校策划项目,直到最近才开始涉足Python-我遇到了一个我根本不知道的问题?我看过其他人的问题,这些人似乎和我有同样的问题,但似乎更具选择性,我的代码也有所不同。谁能告诉我为什么即使我知道一个事实rnumber == tnumber时,只要我回答这个问题,它都会立即跳到“重试!”? (使用Python 3.4.2)。

#Generates the random number module
import random
#Creates the variable in which I store my random number
rnumber = random.randint(0,9999)
#Delete this code when complete
print (rnumber)
#Number of tries
numot = 0
#Asks for user input, on what they think the number is
tnumber = input("Guess the four digit number. ")
type(tnumber)
#Compares their number to the random number
if tnumber == rnumber:
    print ("You win!")
elif rnumber != tnumber:
    print ("Try again!")
    numot = numot+1

1 个答案:

答案 0 :(得分:3)

您需要将输入内容设置为int,以便将其视为数字,请尝试

#Generates the random number module
import random
#Creates the variable in which I store my random number
rnumber = random.randint(0,9999)
#Delete this code when complete
print (rnumber)
#Number of tries
numot = 0
#Asks for user input, on what they think the number is
tnumber = int(input("Guess the four digit number. "))
#Compares their number to the random number
if tnumber == rnumber:
    print ("You win!")
else rnumber != tnumber:
    print ("Try again!")
    numot = numot+1