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
python小练习:读入一个考试得分,判断这个分数是哪个等级,并输出,考虑异常场景的更多相关文章
- python小练习:使用循环和函数实现一个摇骰子小游戏。游戏规则如下:游戏开始,首先玩家选择Big or Small(押大小),选择完成后开始摇三个骰子,计算总值,11<=总值<=18为“大”,3<=总值<=10为“小”。然后告诉玩家猜对或者是猜错的结果。
python小练习:使用循环和函数实现一个摇骰子小游戏.游戏规则如下:游戏开始,首先玩家选择Big or Small(押大小),选择完成后开始摇三个骰子,计算总值,11<=总值<=18为“ ...
- python小练习1:设计这样一个函数,在桌面的文件夹上创建10个文本,以数字给它们命名。
python小练习1:设计这样一个函数,在桌面的文件夹上创建10个文本,以数字给它们命名. 使用for循环即可实现: for name in range(1,11): desktop_path='C: ...
- Day1:第一个python小程序
Day1:第一个python小程序与开发工具Pycharm 一.Hello World C:\Users\wenxh>python Python 3.6.2 (v3.6.2:5fd33b5, J ...
- 第一个python小脚本
第一个python小实验 前言 作为一个工作1年的linux运维搬砖师来说,发现没点开发能力真的是不好混啊.于是下定决心学习python! 直接上刚写的语句(大神莫鄙视) 通过控制台输入一个账号密码, ...
- python 一个二维数组和一个整数,判断数组中是否含有该整数
在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序. 请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数. de ...
- Python基础(一):程序输入输出、判断合法用户、编写判断成绩的程序
一.程序输入输出 目标: 编写login.py脚本,实现以下目标: 提示用户输入用户名 将用户名保存在变量中 在屏幕上显示欢迎用户的信息 方案: 编写程序时,很多情况下都需要程序与用户交互.在pyth ...
- Python小游戏、小程序
python 小游戏之摇骰子猜大小 python 实现一个双色球生成程序 python-循环与判断练习题
- Python小项目之五子棋
1.项目简介 在刚刚学习完python套接字的时候做的一个五子棋小游戏,可以在局域网内双人对战,也可以和电脑对战 2.实现思路 局域网对战 对于局域网功能来说,首先建立连接(tcp),然后每次下棋时将 ...
- python小技巧 小知识
python小技巧 小知识 python系统变量(修改调用shell命令路径)或用户空间说明 20150418 python调用系统命令,报找不到.怎么办? 类似执行shell的: [ -f /etc ...
随机推荐
- Vue项目
1.新建Vue项目:vue init webpack projectName 2.vue-router模块 1.安装vue-router模块:npm install vue-router --save ...
- oracle中索引快速全扫描和索引全扫描的区别
当进行index full scan的时候 oracle定位到索引的root block,然后到branch block(如果有的话),再定位到第一个leaf block, 然后根据leaf bloc ...
- 【SQL】group by 和order by 的区别。
group by 分组,比如group by name.那么重复name就显示一遍,即同样内容归类显示一遍. group by ……having……——(where 后不可以接聚合函数,而having ...
- 让网站不去请求favicon.ico图标
让网站不去请求favicon.ico图标 favicon.ico 图标用于收藏夹图标和浏览器标签上的显示,如果不设置,浏览器会请求网站根目录的这个图标,如果网站根目录也没有这图标会产生 404.出于优 ...
- 调用finecms栏目多图怎么实现
finecms栏目自定义字段添加图集怎么调用出来?已经上传两张图片了,点击可以预览图片,前端显示不了,如下图所示.调用栏目多图这个要涉及到二次开发,首先要先添加栏目自定义字段,设为文件的格式,然后可以 ...
- Redis入门到高可用(九)——无序set
一.结构 特点:无序,无重复,支持集合间操作 二.主要API smembers : 无序:(会阻塞)小心使用,可用sscan代替 spop: 从集合中弹出元素,每次只能弹出一个: 三.实战 抽奖系统 ...
- SparkSql常用语句
-连接sparksql: cd /home/mr/spark/bin ./beeline !connect jdbc:hive2://hostname:port --切换数据库 use databas ...
- emq共享订阅
emqtt 试用(二)验证 emq 和 mosquito 的共享订阅 1. 多个订阅者都订阅以下主题形式 clientA 订阅 $queue/topic 发布主题名称为 topic1 clien ...
- Entity Framework Code First(概要)
EF开源项目地址:https://github.com/aspnet/EntityFramework6 MSDN :https://msdn.microsoft.com/en-us/library/a ...
- 封装及调用fetch
一.封装fetch 创建fetch/index.js import 'whatwg-fetch' import 'es6-promise' export function get(url) { let ...