鱼C论坛

 找回密码
 立即注册
查看: 3413|回复: 1

求助

[复制链接]
发表于 2022-9-10 13:17:27 | 显示全部楼层 |阅读模式

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

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

x
帮忙找下问题,为什么只输入了一次就结束了
  1. import random
  2. times = 3
  3. no = random.randint(1,100)
  4. name = input("请输入您的姓名:")
  5. print('你好,' + name + '!''开始答题')
  6. temp = input("1+1:")
  7. guess = int(temp)
  8. while guess != 2:
  9.     if guess < 2:
  10.             print('小了')
  11.     if guess > 2:
  12.             print("大了")
  13.     temp = input("新的填入:")
  14.     guess = int(temp)
  15. print("通知:正确")

  16. temp = input("有时候运气也是实力的一种,猜1~100的数:")
  17. yet = int(temp)
  18. while (yet != no) and (times > 0):
  19.     if yet < no:
  20.             print('小了')
  21.     if yet > no:
  22.             print("大了")
  23.     if times > 0:
  24.             print("再试一次吧:", end=" ")
  25.     else:
  26.             print("机会用光咯T_T")
  27.             temp = input("新的填入:")
  28.             yet = int(temp)
  29.     times = times - 1  # 用户每输入一次,可用机会就-1
  30. print("通知:正确")

复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-9-10 13:41:35 | 显示全部楼层
圈出来的这几行代码不应该放在else中,应该放在第24行的if语句中
ps : 你的代码不止这一个问题,帮你改一下
  1. import random
  2. times = 3
  3. no = random.randint(1,100)
  4. name = input("请输入您的姓名:")
  5. print('你好,' + name + '!''开始答题')
  6. temp = input("1+1:")
  7. guess = int(temp)
  8. while guess != 2:
  9.     if guess < 2:
  10.             print('小了')
  11.     if guess > 2:
  12.             print("大了")
  13.     temp = input("新的填入:")
  14.     guess = int(temp)
  15. print("通知:正确")

  16. temp = input("有时候运气也是实力的一种,猜1~100的数:")
  17. yet = int(temp)
  18. while True:
  19.     if yet < no:
  20.             print('小了')
  21.     if yet > no:
  22.             print("大了")
  23.     if yet == no:
  24.             print("通知:正确")
  25.             break
  26.     if times > 0:
  27.             print("再试一次吧")
  28.             temp = input("新的填入:")
  29.             yet = int(temp)
  30.     else:
  31.             print("机会用光咯T_T")
  32.             break
  33.     times = times - 1  # 用户每输入一次,可用机会就-1
复制代码
屏幕截图 2022-09-10 133731.jpg
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-23 11:15

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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