用python实现一个猜数游戏
时间: 2023-12-27 07:04:58 浏览: 80
好的,我可以为您提供一个猜数字的游戏的Python程序。以下是程序的代码:
```
import random
print("Welcome to the Guess the Number Game!")
# Generate a random number between 1 and 100
number = random.randint(1, 100)
# Initialize the number of guesses
num_guesses = 0
# Loop until the user guesses the correct number
while True:
# Get the user's guess
guess = int(input("Guess a number between 1 and 100: "))
# Increment the number of guesses
num_guesses += 1
# Check if the guess is correct
if guess == number:
print("Congratulations! You guessed the number in", num_guesses, "guesses.")
break
# Check if the guess is too high
elif guess > number:
print("Your guess is too high. Try again.")
# If the guess is too low
else:
print("Your guess is too low. Try again.")
```
运行该程序,将会提示玩家输入一个 1 至 100 之间的数值,程序将会逐步提示猜测是否正确,并统计玩家猜测数的次数,直到猜中。
阅读全文