赞
踩
方法一:使用正则表达式
import re
while True:
score = (input("请输入成绩:"))
if score.isdigit():
score = int(score)
elif (re.match('^\d+\.\d+$',score)):
score = float(score)
else:
if score.lower()=='q':
break
else:
print("请输入0到100数字")
continue
if 0<=score<=100:
if 90<=score<=100:
print("A")
elif 80<=score<90:
print("B")
elif 60<=score<80:
print("C")
elif 0<=score<60:
print("D")
else:
print("输入有误")
continue
二、使用isdigit()判断是否为整数实现
while True:
score = (input("请输入成绩:"))
if len(str(score))<=5:
if score.lower() == 'q':
break
elif score=='':
continue
_score=score.replace('.','',1)
if _score.isdigit():
if _score == score:
score = int(score)
else:
score = float(score)
if 90<=score<=100:
print("A")
elif 80<=score<90:
print("B")
elif 60<=score<80:
print("C")
elif 0<=score<60:
print("D")
else:
print("输入有误")
continue
else:
print("请输入0到100数字,小数点最多2位")
continue
方式三:使用异常处理
def Score():
while True:
score = (input("请输入成绩:"))
if score.lower() == 'q':
break
try:
score = float(score)
if 90<=score<=100:
print("A")
elif 80<=score<90:
print("B")
elif 60<=score<80:
print("C")
elif 0<=score<60:
print("D")
else:
print('请输入0到100之间')
except ValueError as e:
print('输入有误,错误信息为:%s'%e)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。