鱼C论坛

 找回密码
 立即注册
查看: 876|回复: 5

条件分支的问题

[复制链接]
发表于 2018-7-25 21:58:40 | 显示全部楼层 |阅读模式

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

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

x
  1. print('请输入分数:')
  2. temp=input()
  3. while temp.isdigit():
  4.        score=int(temp)
  5.        if 0<=score<=100:
  6.           if 90<=score<=100:
  7.              print('A')
  8.           elif 80<=score<90:
  9.              print('B')
  10.           elif 60<=score<80:
  11.              print('C')
  12.           elif 0<=score<60:
  13.               print('D')
  14.        else:
  15.            print('抱歉您的输入有误,请重新输入:')
  16.            
复制代码





我打印101的时候系统就一直打印“抱歉您的输入有误,请重新输入:“
我是想让他打印一遍,然后再输入数据的时候可以执行上面的循环
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2018-7-25 22:10:05 | 显示全部楼层
temp = float(input("输入成绩:"))
while temp>100:
    temp =float(input("抱歉您的输入有误,请重新输入:"))
if temp >=90 :
    print("成绩为A")
elif 90> temp >=80 :
    print("成绩为B")
elif 80> temp >=60 :
    print("成绩为C")
elif 60> temp >=0 :
    print("成绩为D")
    print("成绩为B")
elif 80> temp >=60 :
    print("成绩为C")
elif 60> temp >=0 :
    print("成绩为D")
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-7-25 22:10:45 | 显示全部楼层
  1. temp = float(input("输入成绩:"))
  2. while temp>100:
  3.     temp =float(input("抱歉您的输入有误,请重新输入:"))
  4. if temp >=90 :
  5.     print("成绩为A")
  6. elif 90> temp >=80 :
  7.     print("成绩为B")
  8. elif 80> temp >=60 :
  9.     print("成绩为C")
  10. elif 60> temp >=0 :
  11.     print("成绩为D")
  12.     print("成绩为B")
  13. elif 80> temp >=60 :
  14.     print("成绩为C")
  15. elif 60> temp >=0 :
  16.     print("成绩为D")
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-7-25 22:16:19 | 显示全部楼层
input的所有内容都是字符串,所以你得到的结果都是else。
开头你可以改为:temp = int(input("输入成绩:"))
要输入有小数的话,就是:temp = float(input("输入成绩:"))
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-7-25 22:23:05 | 显示全部楼层
本帖最后由 无符号整形 于 2018-7-26 07:50 编辑

你把int(你的条件)与str(input提供的数据)比较,当然是false,然后进入else,然后你又没有让用户输入来修改temp,所以就会无限循环。
改进版:
  1. print('请输入分数:')
  2. temp=int(input())
  3. while str(temp).isdigit():
  4.        score=int(temp)
  5.        if 0<=score<=100:
  6.           if 90<=score<=100:
  7.              print('A')
  8.           elif 80<=score<90:
  9.              print('B')
  10.           elif 60<=score<80:
  11.              print('C')
  12.           elif 0<=score<60:
  13.               print('D')
  14.        else:
  15.            temp=int(input('抱歉您的输入有误,请重新输入:'))
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-7-25 22:32:43 | 显示全部楼层
qazat9 发表于 2018-7-25 22:16
input的所有内容都是字符串,所以你得到的结果都是else。
开头你可以改为:temp = int(input(" ...

没注意你把score=int(temp)放到while循环内了,while循环开始前都判断temp是字符串
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-9-10 08:52

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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