我正在研究在youtube视频中发现的猜谜游戏,以学习python。 我想添加一个功能,通过输入数字而不是字母来阻止用户使我的游戏崩溃。
这是我的代码。
import random
num_of_guesses = 0
print("Hello, what is your name?")
name = input("Enter Name")
number = random.randint(1,20)
print("Hello",name, "I am thinking of a number between 1 and 20")
while num_of_guesses < 10:
print("Take a guess")
guess = input()
guess = int(guess)
if guess != int:
print("Please enter a number")
num_of_guesses = num_of_guesses + 1
if guess < number:
print("Number too low")
if guess > number:
print("Number too high")
if guess == number:
break
if guess == number:
num_of_guesses = str(num_of_guesses)
print("You won! In ", num_of_guesses, "guesses.")
我添加了
guess = int(guess)
if guess != int:
print("Please enter a number")
将代码段分段到while循环,希望它可以解决我的问题,但不能解决问题。