写一个游戏小代码。
Sure, here's a simple game code in Python:
import random
def guess_number():
number = random.randint(1, 100)
attempts = 0
print("Welcome to the Number Guessing Game!")
print("I'm thinking of a number between 1 and 100.")
while True:
guess = int(input("Take a guess: "))
attempts += 1
if guess < number:
print("Too low! Try again.")
elif guess > number:
print("Too high! Try again.")
else:
print(f"Congratulations! You guessed the number in {attempts} attempts.")
break
guess_number()
In this game, the computer generates a random number between 1 and 100. The player needs to guess the number, and the computer provides hints if the guess is too high or too low. The game continues until the player guesses the correct number. The code keeps track of the number of attempts made by the player.
原文地址: https://www.cveoy.top/t/topic/hFFv 著作权归作者所有。请勿转载和采集!