问题求助
本帖最后由 代码小白liu 于 2022-4-10 12:56 编辑import random
importeasygui as g
secret = random.randint(1,10)
times = 3
g.msgbox ("欢迎进入第一个小游戏!!!")
msg = "不妨猜下我心里想的是哪个数字?"
title = "猜数字小游戏"
guess = g.integerbox(msg,title,lowerbound = 1,upperbound = 10)
while guess != secret and times >0:
if guess == secret:
g.msgbox("恭喜你猜对了")
break
elif guess >secret:
guess = g.integerbox(f"数字大了,还剩下{times-1}次机会",title,lowerbound = 1,upperbound = 10)
else:
guess = g.integerbox(f"数字小了,还剩下{times-1}次机会",title,lowerbound = 1,upperbound = 10)
times -=1
g.msgbox("游戏结束!!!")有两个疑问,为什么猜对时不能输出“恭喜你猜对了”, 剩余0次机会还能在输入一次,而没结束循环
我修改了下你的代码
import random
import easygui as g
secret = random.randint(1,10)
times = 3
g.msgbox ("欢迎进入第一个小游戏!!!")
msg = "不妨猜下我心里想的是哪个数字(1-10之间)?"
title = "猜数字小游戏"
guess = g.integerbox(msg,title,lowerbound=1,upperbound=10)
while guess != secret and times >0:
times -= 1
if guess == secret:
g.msgbox("恭喜你猜对了")
break
if times > 0:
if guess >secret:
guess = g.integerbox(f"数字大了,还剩下{times}次机会(1-10之间)",title,lowerbound=1,upperbound=10)
else:
guess = g.integerbox(f"数字小了,还剩下{times}次机会(1-10之间)",title,lowerbound=1,upperbound=10)
else:
break
g.msgbox("游戏结束!!!")
QQ小鱼 发表于 2022-4-10 11:42
我修改了下你的代码
能分析下我的哪里逻辑不对么 本帖最后由 QQ小鱼 于 2022-4-10 23:39 编辑
1 代码小白liu 发表于 2022-4-10 13:46
能分析下我的哪里逻辑不对么
1、我现在完全明白了,首先你time =0次,这个事情,你自己看代码吧!很简单,最好你还是复习下以前的知识。
2、为啥不会出现,‘恭喜比猜对了’,主要是whilez里面的顺序,我个人认为while里‘恭喜你猜对了’必须放到最后,你还没有理解easyguiyi
import random
import easygui as g
secret = random.randint(1,4)
g.msgbox("欢迎进入第一个小游戏!!!")
times = 3
msg = "不妨猜下我心里想的是哪个数字(1-10之间)?"
title = "猜数字小游戏"
guess = g.integerbox(msg, title, lowerbound=1, upperbound=10)
while times > 0 and guess != secret:
times -= 1
if times > 0:
if guess > secret:
guess = g.integerbox(f"数字大了,还剩下{times}次机会(1-10之间)",title,lowerbound=1,upperbound=10)
else:
guess = g.integerbox(f"数字小了,还剩下{times}次机会(1-10之间)",title,lowerbound=1,upperbound=10)
else:
break
if guess == secret:
g.msgbox("恭喜你猜对了")
break
g.msgbox("游戏结束!!!")
页:
[1]