555这个应该怎么改啊
import randomsecret=random.randint(1,20)
temp=input("在1到20中猜猜我心里想的数字是几:")
times=1
while type(temp)!= type(1):
print("你以为我不知道你要干什么吗?")
guess=int(temp)
while(guess!=secret) and (times<3):
if guess>secret:
print("大了大了")
else:
print("小了小了")
temp=input("再来猜一猜吧:")
guess=int(temp)
times=times+1
if (times<=3) and (guess==secret):
print("竟然对了吗,可恶。")
else:
print("哈哈哈哈哈哈哈还不对吗,都给了你三次机会了,不玩了不玩了。"){:10_282:} 鱼油代码主要问题是:while type(temp)!= type(1): 这一句永远成立(input()拿到的一定是字符串),而且你也没在这个循环里更新 temp,会直接死循环。
修改一下就好:
import random
secret = random.randint(1, 20)
for i in range(3):
guess = int(input("在1到20中猜猜我心里想的数字是几:"))
if guess == secret:
print("竟然对了吗,可恶。")
break
print("大了大了" if guess > secret else "小了小了")
else:
print("哈哈哈哈哈哈哈还不对吗,都给了你三次机会了,不玩了不玩了。") 你的代码有几个问题需要修正:
1. 第一个 `while` 循环会导致无限循环,因为 `input()` 返回的永远是字符串类型,不可能等于整数类型。
2. 循环逻辑和尝试次数计算有误,可能导致机会次数不对。
3. 最后条件判断不完整。
以下是修改后的代码:
import random
secret = random.randint(1, 20)
times = 0
max_times = 3
while times < max_times:
if times == 0:
temp = input("在1到20中猜猜我心里想的数字是几:")
else:
temp = input("再来猜一猜吧:")
try:
guess = int(temp)
except ValueError:
print("输入无效,请输入一个整数。")
continue
times += 1
if guess == secret:
print("恭喜你,猜对了!")
break
elif guess > secret:
print("大了大了")
else:
print("小了小了")
if times < max_times:
print(f"你还有{max_times - times}次机会。")
if times == max_times and guess != secret:
print(f"很遗憾,你没有猜对。正确的数字是{secret}。")
主要修改点:
- 移除了第一个 `while` 循环,改用 try-except 处理输入转换,避免无限循环。
- 使用 times 记录尝试次数,从 0 开始,最多 3 次。
- 在循环中根据次数显示不同的输入提示。
- 如果猜中,提前结束循环;如果次数用完还没猜中,显示正确数字。
这样,玩家有三次机会猜数字,输入非整数时不会浪费机会,并会提示重新输入。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 改了一下,好像可以了:
import random
secret=random.randint(1,20)
temp=input("在1到20中猜猜我心里想的数字是几:")
times=1
if temp.isdigit():
guess=int(temp)
while(guess!=secret) and (times<3):
if guess>secret:
print("大了大了")
else:
print("小了小了")
temp=input("再来猜一猜吧:")
guess=int(temp)
times=times=1
if (times<=3) and (guess==secret):
print("竟然对了吗,可恶。")
else:
print("哈哈哈哈哈哈哈还不对吗,都给了你三次机会了,不玩了不玩了。")
else:
print("你在耍我吗?") Evansolo 发表于 2026-01-28 21:08
改了一下,好像可以了:
import random
secret=rand ...
很好,你的思路是正确的!使用 isdigit() 来判断输入是否为数字是很实用的方法。不过,我注意到你修改的代码中还有几个小问题:
1. 在第 10 行有一个错误: times=times=1 应该是 times=times+1
2. 最后的 if 条件不完整
3. 如果第一次输入的不是数字,程序没有处理
这里是我根据你的思路修正后的版本:
import random
secret=random.randint(1,20)
temp=input("在1到20中猜猜我心里想的数字是几:")
times=1
if temp.isdigit():
guess=int(temp)
else:
print("你以为我不知道你要干什么吗?")
guess = 0# 给guess一个初始值,避免后面未定义错误
while(guess!=secret) and (times<3):
if guess>secret:
print("大了大了")
else:
print("小了小了")
temp=input("再来猜一猜吧:")
if temp.isdigit():
guess=int(temp)
times=times+1
else:
print("你以为我不知道你要干什么吗?")
# 不增加尝试次数,让用户重新输入
continue
# 判断最终结果
if guess==secret:
print(f"恭喜你,猜对了!你用了{times}次机会。")
else:
print(f"很遗憾,你没有猜对。正确的数字是{secret}。")
主要改进:
1. 处理了第一次输入非数字的情况
2. 修复了 times=times=1 的错误
3. 在循环中每次输入都检查是否为数字
4. 如果输入非数字,不增加尝试次数,让用户重新输入
5. 添加了完整的结果判断
这样修改后,程序更健壮,能处理各种输入情况,并且符合你原本的思路。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 不二如是 发表于 2026-1-29 20:25
鱼油代码主要问题是:while type(temp)!= type(1): 这一句永远成立(input()拿到的一定是字符串),而且你 ...
OKOK感谢{:10_282:}我改了第二次之后发现下面第三组if-else也有点小问题,在while里循环了。{:10_282:}又改了一下:
import random
secret=random.randint(1,20)
temp=input("在1到20中猜猜我心里想的数字是几:")
times=1
if temp.isdigit():
guess=int(temp)
while(guess!=secret) and (times<3):
if guess>secret:
print("大了大了")
else:
print("小了小了")
temp=input("再来猜一猜吧:")
guess=int(temp)
times=times+1
if (times<=3) and (guess==secret):
print("竟然对了吗,可恶。")
else:
print("哈哈哈哈哈哈哈还不对吗,都给了你三次机会了,不玩了不玩了。")
else:
print("你在耍我吗?")
页:
[1]