- js27115 的博客
Python简单猜数字2.0
- @ 2026-2-13 15:55:30
2月16日补评:为了做这个我电脑都过载了,你们相信吗。现在我每打一段字就要摸一下主机有没有热,我真服了。
2月25日补评:已更新
以下代码全是我在Python课里学的,后面我对代码做了一个稍稍的改进。有一次改进的时候,突发奇想把continue加进去了,结果代码刚运行到那就废了。这个故事告诉我们不要太装逼,装逼伤身体,更伤编译器。
目前这个代码只有Python版的,c++版的就等我基本功练完了再说吧。
好了不说废话了步入正题。
import random
a = random.randint(1, 100)
attempts = 0
print("Let's guess numbers!")
while True:
try:
b = int(input("Enter a number from 1 to 100: "))
attempts += 1
if b < 1 or b > 100:
print("The number is out of range.")
elif b < a:
print("The number is too small.")
elif b > a:
print("The number is too big.")
else:
print("Congratulations! You guessed it right.")
print(f"You have tried {attempts} times.")
break
except ValueError:
print("Please enter a valid integer!")
请下载thonny用于运行此代码。直接喂给豆包也可以。
要是突然报错了不关我的事
另外:
老铁们你们觉得c++人能看懂Python代码吗。
再另外:
部分内容根据豆包AI改动而成。