我在Python中的“猜数字”游戏不起作用

时间:2019-10-06 20:29:09

标签: python

我正在尝试用Python制作一个基本的游戏,用户必须尝试猜出1到20之间的一个数字。但是,无论输入什么,程序都说猜测太高了。

import random

choice = ""

while choice != "exit":
    print("Welcome to Guess The Number!")
    print("Try to guess the number from 1 to 20: ")

    for x in range(1):
        num = random.randint(1, 21)

    guess = raw_input()
    guesses = 0

    while(guess != num):
        if guess < num:
            guess = raw_input("Too low. Try again! ")
            guesses += 1
            print(num)
        else:
            guess = raw_input("Too high. Try again! ")
            guesses += 1
            print(num)

    if(guess == num):
        guesses += 1
        print("Correct!\nNumber of guesses: " + guesses)
        choice = raw_input("Press Enter to play again, or type \"exit\" to exit. ")

任何帮助将不胜感激。谢谢。

0 个答案:

没有答案