zoukankan      html  css  js  c++  java
  • python小练习:读入一个考试得分,判断这个分数是哪个等级,并输出,考虑异常场景

    读入一个考试得分,判断这个分数是哪个等级,并输出。

    等级:》=90 优 ,>=80且小于90 良,》=70 且小于80,中,》=60且<70及格 
    《60 不及格

    覆盖场景:

    考虑字符类型(str,float)

    数字范围(0-100)以外的

    小数98.99等

    代码:

    #encoding=utf-8
    while True:
        try:
            score = raw_input("input the score,'exit' to stop: >>:")
            #print type(score)
            #print isinstance(score,str)

            if "exit" in score:
                break
            else:
                score = float(score)
                print "score:",score
                if score >= 90 and score <=100:
                    print u"优"
                elif score>=80 and score <90:
                    print u"良"
                elif score >=70 and score <80:
                    print u"中"
                elif score >= 60 and score <70:
                    print u"及格"
                elif score >=0 and score <60:
                    print u"不及格"
                else:
                    print u"请重新输入!"
        except Exception,e:
            print e
            print u"请重新输入!"
            continue

    结果:

    D:>python test.py
    input the score,'exit' to stop: >>:90
    score: 90.0

    input the score,'exit' to stop: >>:100
    score: 100.0

    input the score,'exit' to stop: >>:89
    score: 89.0

    input the score,'exit' to stop: >>:78
    score: 78.0

    input the score,'exit' to stop: >>:67
    score: 67.0
    及格
    input the score,'exit' to stop: >>:56
    score: 56.0
    不及格
    input the score,'exit' to stop: >>:1
    score: 1.0
    不及格
    input the score,'exit' to stop: >>:0
    score: 0.0
    不及格
    input the score,'exit' to stop: >>:-2
    score: -2.0
    请重新输入!
    input the score,'exit' to stop: >>:9.3
    score: 9.3
    不及格
    input the score,'exit' to stop: >>:8.88
    score: 8.88
    不及格
    input the score,'exit' to stop: >>:90.89
    score: 90.89

    input the score,'exit' to stop: >>:101
    score: 101.0
    请重新输入!
    input the score,'exit' to stop: >>:200
    score: 200.0
    请重新输入!
    input the score,'exit' to stop: >>:stop
    could not convert string to float: stop
    请重新输入!
    input the score,'exit' to stop: >>:sdlfj
    could not convert string to float: sdlfj
    请重新输入!
    input the score,'exit' to stop: >>:*&%$
    could not convert string to float: *&%$
    请重新输入!
    input the score,'exit' to stop: >>:exit

  • 相关阅读:
    c#常用正则表达式
    亲密接触Discuz!NT之架构篇:优良架构 方便网站整合与二次开发
    即时对话,在线对话,QQ,MSN,UC,popo
    C#事务处理
    正则表达式中的特殊字符
    9:38 2009729
    16:43 200981 缓解疲劳的七大唱片 免费短信
    复选框 全选
    9:05 2009721
    9:34 2009728
  • 原文地址:https://www.cnblogs.com/xiaxiaoxu/p/10034882.html
Copyright © 2011-2022 走看看