为什么我不能将输入与Python

时间:2017-12-26 01:30:43

标签: python

所以我基本上想在if语句中比较“Number”和“Guess”,但无论它说什么都不匹配(我得到了其他响应,不包括在这里)。即使我复制了他们不匹配的随机数。 提前谢谢!

import time
def the_start():
    points = 0
    attempt = 1
    print("Attempt:",attempt)
    print("Your goal is to guess a number between 1 and 10 - Points:",points)
    time.sleep(2)
    attempt = attempt + 1
    number = random.randint(0,10)
    print(number)
    guess = input("What is your guess?  :")
    time.sleep(2)

    if guess == number:
        points = points + 1
        print("OMG YOU WERE RIGHT! Here, have some fake cheers! *cheer*")
        time.sleep(5)

1 个答案:

答案 0 :(得分:1)

猜是一个字符串。您需要转换字符串并处理错误条件。 int()会将字符串转换为整数,但如果字符串不是纯数字,它将引发异常。