求助
#p3_1.pytemp=input("猜猜我心里的数字")
guess=int(temp)
while guess==1:
if guess>1:
print("大了")
else:
print("小了")
temp=input("请再尝试吧:")
guess=int(temp)
print("你是我肚子的蛔虫")
这个while guess==1 循环的是什么为什么输入1的时候反而告诉我小了?
#p3_1.py
temp=input("猜猜我心里的数字")
guess=int(temp)
while guess!=1:
if guess>1:
print("大了")
else:
print("小了")
temp=input("请再尝试吧:")
guess=int(temp)
print("你是我肚子的蛔虫")
这个while guess!=就可以正常的循环
为什么呢?他们这个分别循环了什么? 正常来说我循环的应该是等于1,为什么我While guess==1循环时会出现小于1的情况,我打1反而告诉我小 他是不是循环下面那个你是我肚子里的蛔虫了,没有循环上面的代码 叶百旗 发表于 2023-3-3 15:01
他是不是循环下面那个你是我肚子里的蛔虫了,没有循环上面的代码
你的分支 不就设置了2个吗>1 之外的 都进入另一个分支不就是包含=1 是因为他判断不大于一的时候(else)还没来得及退出,于是打印小了
第二个问题的解答:因为你输入了 1,没进循环,直接打印了 本帖最后由 sfqxx 于 2023-3-3 17:54 编辑
因为没有进循环
改进代码:temp=input("猜猜我心里的数字")
guess=int(temp)
while guess==1:
if guess>1:
print("大了")
elif guess<1:
print("小了")
else:
print("你是我肚子里的蛔虫")
break#退出循环
temp=input("请再尝试吧:")
guess=int(temp)
第二个问题,没有进循环就直接print那条语句了
页:
[1]