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

时间:2017-12-20 00:28:38

标签: 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版本是否有错(上面发布)。

1 个答案:

答案 0 :(得分:0)

请进行以下更改:

if (again == 1):
    control = True
    main=True
    user = ("yes")