帮我做一个在控制台就能玩的200行代码的python小游戏
时间: 2024-02-29 16:53:19 浏览: 148
使用Python写一个小游戏
当然可以,我可以为您提供一个猜数字的小游戏,代码如下:
```python
import random
def play_game():
print("Welcome to the Number Guessing Game!")
print("I'm thinking of a number between 1 and 100.")
print("You have 7 guesses to find the number.")
secret_number = random.randint(1, 100)
num_guesses = 0
while num_guesses < 7:
try:
guess = int(input("Enter your guess: "))
except ValueError:
print("Invalid input. Please enter a number between 1 and 100.")
continue
num_guesses += 1
if guess == secret_number:
print(f"Congratulations! You guessed the number in {num_guesses} tries.")
return
elif guess < secret_number:
print("Too low. Guess again.")
else:
print("Too high. Guess again.")
print(f"Sorry, you ran out of guesses. The secret number was {secret_number}.")
play_game()
```
这个游戏会让用户猜一个 1 到 100 之间的秘密数字,用户有 7 次机会来猜到这个数字。如果用户猜对了,游戏结束并显示所用的猜测次数。如果用户没有在 7 次机会内猜对,游戏结束并显示正确的数字。
阅读全文