鱼C论坛

 找回密码
 立即注册
查看: 1522|回复: 7

[已解决]如何处理异常

[复制链接]
发表于 2020-5-12 14:30:35 | 显示全部楼层
本帖最后由 wuqramy 于 2020-5-12 14:57 编辑

用try except语句:
  1. score = input ("请输入你的分数,退出输入EXIT")
  2. while score != 'EXIT':
  3.     while True:
  4.         try:
  5.             score = int (score)
  6.             break
  7.         except:
  8.             score = input('输入错误!请重新输入:')

  9.     if score < 60:
  10.             print("D")

  11.     if 60 <= score < 80:
  12.             print("C")

  13.     if 80 <= score < 90:
  14.             print("B")

  15.     if 90 <= score < 100:
  16.             print("A")

  17.     if score == 100:
  18.             print("S")

  19.     score = input("请输入你的分数:")
复制代码

还可以用字符串方法:
  1. score = input ("请输入你的分数,退出输入EXIT")
  2. while score != 'EXIT':
  3.     while not score.isdigit():
  4.         score = input ("输入错误,请再次请输入你的分数,退出输入EXIT")
  5.         if score == 'EXIT':
  6.             break
  7.     if score == 'EXIT':
  8.             break
  9.     score = int(score)
  10.     if score < 60:
  11.             print("D")

  12.     if 60 <= score < 80:
  13.             print("C")

  14.     if 80 <= score < 90:
  15.             print("B")

  16.     if 90 <= score < 100:
  17.             print("A")

  18.     if score == 100:
  19.             print("S")

  20.     score = input("请输入你的分数:")
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-5-12 14:57:42 | 显示全部楼层
sunrise085 发表于 2020-5-12 14:39
你的程序是有问题滴!
若打算输入 EXIT 退出,却错输成了其他字母,则必须要再输入一个分数之后才能输入 ...


改好了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-4-2 04:36

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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