我猜数字游戏不会回到开始

2024-05-12 23:00:54 发布

您现在位置:Python中文网/ 问答频道 /正文

import random
control = True
main = True
count = 0

user = input("Would you like to play Guess The Number?")
if (user == "yes"):
    while (control == True):
        randgen = random.randrange(0, 100)
        print("Guess a random number")
        while main == True:
            number = int(input())
            if (number == randgen):
                print("Great Job you guessed the correct number")
                print("You have tried ", count, "time(s)")
                main = False
                control = False

            if (number < randgen):
                count += 1
                print("Your number is smaller than the random number")
                print("You are at ", count, "trie(s)")
                main = True
            if (number > randgen):
                count += 1
                print("Your number is larger than the random number")
                print("You are at ", count, "trie(s)")
                main = True
        again = int(input("Would you like to play again?1 for yes and 2 for no."))
        if (again == 1):
            control = True

            user = ("yes")
        if (again == 2):
            control = False
            print ("Ok bye bye")
            ##user ("no")

if (user == "no"):
    print ("OK then Bye")

这个代码可以工作,除了当我想再玩一次它不工作的部分。我有java的编码背景,这就是为什么我知道一些代码,但我用java做了猜数字游戏,我无法找出我的python版本(见上文)有什么问题。你知道吗


Tags: theyoutruenumberinputifmaincount