鱼C论坛

 找回密码
 立即注册
查看: 1808|回复: 4

为什么会出现第一结果?

[复制链接]
发表于 2021-2-2 20:40:24 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
print('------我爱老田-------')
temp = input("请输入1到100的数字:")
guess = int(temp)
times = 3
  
while (guess != 8) and times >0:
    temp = input("继续猜:")
    guess = int(temp)
    times = times -1
    if guess == 8:
        print("你好牛逼啊!")

    else:
        if guess > 8:
            print("大了")
        else:
            print("小了")

print("游戏结束!")
-------------------------------------------------------以上为我写的代码
------我爱老田-------
请输入1到100的数字:4

继续猜:5
小了
继续猜:6
小了
继续猜:9
大了
游戏结束!
>>>
------------------------------------------------以上为我的结果。
问题:
1.我希望三次以后结束循环,为什么4次?
2.红字部分为什么没有“大了”“小了”的提示?
感谢
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-2-2 20:47:22 | 显示全部楼层

  1. print('------我爱老田-------')
  2. temp = input("请输入1到100的数字:")
  3. guess = int(temp)
  4. times = 3
  5.   
  6. while (guess != 8):
  7.     guess = int(temp)
  8.     times = times -1
  9.     if guess == 8:
  10.         print("你好牛逼啊!")

  11.     else:
  12.         if guess > 8:
  13.             print("大了")
  14.         else:
  15.             print("小了")
  16.     if times == 0:
  17.         break
  18.     temp = input("继续猜:")

  19. print("游戏结束!")
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-2-2 20:48:57 | 显示全部楼层
1. while 里面是 3 次,多出来的一次是在 while 外面猜的
2. 你按从上到下按顺序看一遍代码就知道了,程序执行顺序是从上到下的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2021-2-2 21:42:47 | 显示全部楼层
循环里循环了3次,后面还有一次所以你可以将times改为2
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-2-3 12:22:26 | 显示全部楼层
print('------我爱老田-------')
temp = input("请输入1到100的数字:")
guess = int(temp)
times = 2

while (guess != 8) and times > 0:
    if guess == 8:
        print("你好牛逼啊!")

    else:
        if guess > 8:
            print("大了")
        else:
            print("小了")
    temp = input("继续猜:")
    guess = int(temp)
    times = times - 1

print("游戏结束!")
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-17 13:08

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表