python程序设计基础

img


python编写程序接受用户输入分数信息
如果分数在零到100之间

该回答引用文心一言:


score = int(input("请输入分数:"))  
  
if score >= 0 and score <= 100:  
    print("您的分数是:", score)  
else:  
    print("分数必须在零到一百之间!")
try:
    # 使用 input() 函数接收用户输入的分数信息,并使用 float() 函数将其转换为浮点数
    score = float(input("请输入分数: "))
    # 使用 if 语句检查该分数是否在0-100之间,如果不在,则抛出异常信息
    if score < 0 or score > 100:
        raise ValueError("分数必须在0-100之间")
    else:
        # 如果分数在0-100之间,则输出分数
        print("分数为:", score)
except ValueError as e:
    # 捕获 ValueError 异常,并输出异常信息
    print("分数不正确!错误信息:", e)