如何重新运行代码

时间:2016-12-20 02:19:00

标签: python

我正在开发一款非常简单的游戏。如果玩家想再次玩,我无法弄清楚如何重新运行while循环。我尝试将循环变成一个函数,但由于某种原因,这只会破坏我的代码。谢谢!

import random

print ("Welcome to numbfy! Try to guess the correct number! Guess a number between 0 and 10! Enter anything but a letter to exit!")

the_num = random.choice(range(0, 11))


while True:
    try:
        guess = int(input("What's your guess? "))
    except ValueError:
        print ("That's not a number!")
        break

    if guess == the_num:
        print ("You guessed it!")
        break
    elif guess > the_num:
        print ("Go lower!")
    elif guess < the_num:
        print ("Go higher!")

0 个答案:

没有答案