帮我写一个小游戏代码并取个名字
时间: 2024-01-27 10:02:28 浏览: 28
好的,以下是一个简单的猜数字游戏代码,取名为"Guess It":
```python
import random
def guess_number():
num = random.randint(1, 100)
print("Welcome to Guess It!")
print("I'm thinking of a number between 1 and 100. Can you guess it?")
guesses = []
while True:
guess = int(input("Enter your guess: "))
if guess < 1 or guess > 100:
print("Your guess is out of range. Please try again.")
continue
if guess == num:
print("Congratulations! You guessed the number in", len(guesses), "tries.")
break
elif guess < num:
print("Too low. Try again.")
else:
print("Too high. Try again.")
guesses.append(guess)
guess_number()
```
这个游戏会生成一个1到100之间的随机整数,玩家需要通过输入数字猜出这个随机数。游戏会在玩家猜出正确数字时结束,并统计玩家猜测的次数。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)