|
发表于 2020-5-12 14:30:35
|
显示全部楼层
本帖最后由 wuqramy 于 2020-5-12 14:57 编辑
用try except语句:
- score = input ("请输入你的分数,退出输入EXIT")
- while score != 'EXIT':
- while True:
- try:
- score = int (score)
- break
- except:
- score = input('输入错误!请重新输入:')
- if score < 60:
- print("D")
- if 60 <= score < 80:
- print("C")
- if 80 <= score < 90:
- print("B")
- if 90 <= score < 100:
- print("A")
- if score == 100:
- print("S")
- score = input("请输入你的分数:")
复制代码
还可以用字符串方法:
- score = input ("请输入你的分数,退出输入EXIT")
- while score != 'EXIT':
- while not score.isdigit():
- score = input ("输入错误,请再次请输入你的分数,退出输入EXIT")
- if score == 'EXIT':
- break
- if score == 'EXIT':
- break
- score = int(score)
- if score < 60:
- print("D")
- if 60 <= score < 80:
- print("C")
- if 80 <= score < 90:
- print("B")
- if 90 <= score < 100:
- print("A")
- if score == 100:
- print("S")
- score = input("请输入你的分数:")
复制代码 |
|