简体   繁体   English

我的Python猜数字游戏

[英]My Python number guessing game

I have been trying to make a number guessing game for Python and so far it has altogether gone quite well. 我一直在尝试为Python创建一个数字猜谜游戏,到目前为止,它完全进行得很好。 But what keeps bugging me is that it resets the number on every guess so that it is different, but I want it to stay the same and then people can take a while guessing it. 但是令我烦恼的是,它会在每次猜测时重置数字,以使其有所不同,但是我希望它保持不变,然后人们可以花点时间猜测它。

import os
import time

print("this is a random number generator")
number1 =int(input("please pick a NUMBER between 1 and 99"))
from random import randint
number2 = int(randint(1,99))
if number1 == number2:
    print(" congrats! you got it right")
elif number1 < number2:
    print ("too low try again")
elif number1 > number2:
    print ("too high try again")

if number1 not in range(1,99):
    print("you idiot! Pick a number IN THE RANGE")

while number1 != number2:


    print("this is a random number generator")
    number1 =int(input("please pick a NUMBER between 1 and 99"))
    from random import randint
    number2 = int(randint(1,99))
    if number1 == number2:
        print(" congrats! you got it right")
    elif number1 < number2:
        print ("too low try again")
    elif number1 > number2:
        print ("too high try again")

    if number1 not in range(1,99):
        print("you idiot! Pick a number IN THE RANGE")



time.sleep(10)
os.system("exit")

Since you have number2 = int(randint(1,99)) inside your while cycle, then you create a new number each time. 由于您的while周期内有number2 = int(randint(1,99)) ,因此您每次都创建一个新数字。 Put that line outside the while and the number will remain the same until someone guesses it 将该行放在while之外,数字将保持不变,直到有人猜到为止

There are a lot of things wrong with your code : the import statement should be at the beginning, and there is much duplicated code, which makes it hard to read. 您的代码有很多问题: import语句应该放在开头,并且有很多重复的代码,这使得它很难阅读。

Your problem comes from the fact that number2 = int(randint(1,99)) is called at each cycle of the while loop. 您的问题来自于在while循环的每个循环中调用number2 = int(randint(1,99))的事实。

This could be improved to : 可以将其改进为:

import os
import time
from random import randint

print("This is a random number generator")
number2 = int(randint(1,99))
while True:
    try:
        number1 =int(input("Please pick a NUMBER between 1 and 99\n"))
        if number1 not in range(1,99):
            print("You idiot! Pick a number IN THE RANGE")
        if number1 == number2:
            print("Congrats! You got it right")
            break
        elif number1 < number2:
            print ("Too low, try again")
        elif number1 > number2:
            print ("Too high, try again")
    except (ValueError, NameError, SyntaxError):
        print("You idiot! This is not a number")

time.sleep(10)
os.system("exit")

Also, no need to import from random import randint everytime you need randint() . 另外,每次需要randint()都不需要from random import randint进行导入。 Put it at the top next to import time 将其放在import time旁边的顶部

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM