亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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 關注
  • 210 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號