为了账号安全,请及时绑定邮箱和手机立即绑定

While 循环不断提示用户提供正确的输入类型

While 循环不断提示用户提供正确的输入类型

慕的地8271018 2021-11-02 15:13:35
我有一个家庭作业问题,我需要在我的函数中添加一个 while 循环,如果最初输入的值不是数字,它将为用户提供 3 次额外的尝试输入另一个值。代码的原始功能是确定三角形或梯形的面积。loopCount = 0# The "while" statement keeps looping until its condition (loopCount<4) made False.while loopCount<4:    # loopCount will increase 1 for each loop    loopCount += 1虽然我什至不确定在我的代码中的哪里适合以上几行。    # This program calculates the area of a triangle or trapezoid    # Statement: print function outputs the statement on screen      print("This program finds the area of a triangle or trapezoid.")    print()    # Module: math module imported    import math    # Determine the objects shape    print("Please enter the shape from the following menu")    print("Triangle = type 1")    print("Trapezoid = type 2")    # If user types a number other than 1 or 2, this will prompt them again to pick a valid choice    user_input = 0    while user_input not in (1,2) :            user_input = int(input("Enter your choice: "))    # Variables: asigns new value to a variable depending on which shape we are caluclating the area of    if (user_input == 1):        print("Alright, you want to calculate the area of a triangle: ")        height = float(input("Please enter the height of the triangle: "))        base = float(input("Please enter the base length of the triangle: "))    if (user_input == 2):        print("Alright, you want to calculate the area of a trapezoid: ")        base_1 = float(input("Please enter the base length of the trapezoid: "))        base_2 = float(input("Please enter the second base length of the trapezoid: "))        height = float(input("Please enter the height of the trapezoid: "))
查看完整描述

1 回答

?
狐的传说

TA贡献1804条经验 获得超3个赞

您可以在输入上使用try/except来处理ValueError何时float()无法将值转换为浮点数或OverflowError“如果参数超出 Python 浮点数范围”。


loopCount = 0

while loopCount < 4:

    try:

        height = float(input("Please enter the height of the triangle: "))

        break

    except:

        print("That is not a number.")

        loopCount += 1 


if loopCount == 4:

    print("You failed to input valid values")

    # return with an error or maybe abort with sys.exit(1)

else:

    print("Great! I can now compute stuff.")

您可以一次检查try块内的所有输入(如果您不关心哪一个特别无效或者您不需要向用户指出它):


loopCount = 0

while loopCount < 4:

    try:

        base_1 = float(input("Please enter the base length of the trapezoid: "))

        base_2 = float(input("Please enter the second base length of the trapezoid: "))

        height = float(input("Please enter the height of the trapezoid: "))

        break

    except:

        print("One of the inputs is not a number.")

        loopCount += 1 


if loopCount == 4:

    print("You failed to input valid values")

    # return with an error or maybe abort with sys.exit(1)

else:

    print("Great! I can now compute stuff.")

为了避免大量重复try-except,我建议创建一个获取浮点输入(或所有输入)的方法,然后从您的主方法中调用它。


查看完整回答
反对 回复 2021-11-02
  • 1 回答
  • 0 关注
  • 169 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号