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

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

嘗試在 python 中執行余弦規則公式,但沒有給出預期的輸出

嘗試在 python 中執行余弦規則公式,但沒有給出預期的輸出

MMTTMM 2023-08-22 10:28:56
如果我選擇缺失角度函數并輸入例如 a = 10、b = 10、c = 10,則角度的預期輸出為 60 度,但程序的輸出為 0.1546...import math, sysdef missingAngle():    sideA = int(input("size of side a = "))    sideB = int(input("size of side b = "))    sideC = int(input("size of side c = "))    answer = math.acos((sideB**2 + sideC**2 - sideA**2) / (2 * sideB * sideC))    return answerdef missingSide():    angleA = int(input("size of angle A = "))    sideB = int(input("size of side b = "))    sideC = int(input("size of side c = "))    answer = math.sqrt(sideB**2 + sideC**2 - 2 * sideB * sideC * math.cos(angleA))    return answermissingSideOrAngle = input("Are you trying to work out the missing angle or side?(Angle/Side) = ")while True:    if missingSideOrAngle.title() == "Angle":        print("Your answer is = " + str(missingAngle()))        sys.exit()    elif missingSideOrAngle.title() == "Side":        print("Your answer is = " + str(missingSide()))        sys.exit()    else:        missingSideOrAngle = input("Please enter a valid string(Angle/Side) = ")
查看完整描述

1 回答

?
偶然的你

TA貢獻1841條經驗 獲得超3個贊

我永遠無法重現產生錯誤 0.1546 的錯誤。


這是您的程序,經過一些調整,將幫助您擴展程序的功能:


import math


def missingAngle():

    sideA = float(input("size of side a = "))

    sideB = float(input("size of side b = "))

    sideC = float(input("size of side c = "))

    try:

        answer = math.acos((sideB**2 + sideC**2 - sideA**2) / (2 * sideB * sideC))

        return f'Your answer is: {math.degrees(answer):.2f} degrees'

    except ValueError:

        return 'Values given cannot form a triangle'


def missingSide():

    angleA = math.radians(float(input("size of angle A (degrees) = ")))

    sideB = float(input("size of side b = "))

    sideC = float(input("size of side c = "))


    answer = math.sqrt(sideB**2 + sideC**2 - 2 * sideB * sideC * math.cos(angleA))

    return f'Your answer is: {answer:.2f} units'



while True:

    missingSideOrAngle = input("Are you trying to work out the missing angle or side?(Angle/Side) = ")

    if missingSideOrAngle.title() == "Angle":

        print(missingAngle())


    elif missingSideOrAngle.title() == "Side":

        print(missingSide())


    else:

        missingSideOrAngle = input("Please enter a valid string(Angle/Side) = ")

我將其設置為一個實際的循環,因此它將讓用戶無休止地運行場景,直到他們點擊Ctrl-C或輸入引發未處理錯誤的內容。我沒有嘗試解釋所有可能的錯誤。


這missingAngle是為了返回學位。它還包含一個“try- except”塊來捕獲如果三邊不能形成真正的三角形(例如:12、45、85)時將發生的錯誤。輸入被轉換為浮點數,因此用戶可以輸入距離或角度的小數,并且返回被格式化為呈現到小數點后兩位。


這應該對您有所幫助。您可能需要為非數字的數字字段的用戶輸入設計錯誤處理,并運行一些測試以查看missingSide用戶輸入可能出現的問題并捕獲這些問題。您可能想要嘗試的一種方法是給它一個 180 度的角度。某些值可能不會產生實際錯誤,但可能會產生意想不到的結果。


祝你好運!


查看完整回答
反對 回復 2023-08-22
  • 1 回答
  • 0 關注
  • 103 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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