1 回答

TA貢獻(xiàn)1841條經(jīng)驗(yàn) 獲得超3個(gè)贊
我永遠(yuǎn)無(wú)法重現(xiàn)產(chǎn)生錯(cuò)誤 0.1546 的錯(cuò)誤。
這是您的程序,經(jīng)過(guò)一些調(diào)整,將幫助您擴(kuò)展程序的功能:
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) = ")
我將其設(shè)置為一個(gè)實(shí)際的循環(huán),因此它將讓用戶(hù)無(wú)休止地運(yùn)行場(chǎng)景,直到他們點(diǎn)擊Ctrl-C或輸入引發(fā)未處理錯(cuò)誤的內(nèi)容。我沒(méi)有嘗試解釋所有可能的錯(cuò)誤。
這missingAngle是為了返回學(xué)位。它還包含一個(gè)“try- except”塊來(lái)捕獲如果三邊不能形成真正的三角形(例如:12、45、85)時(shí)將發(fā)生的錯(cuò)誤。輸入被轉(zhuǎn)換為浮點(diǎn)數(shù),因此用戶(hù)可以輸入距離或角度的小數(shù),并且返回被格式化為呈現(xiàn)到小數(shù)點(diǎn)后兩位。
這應(yīng)該對(duì)您有所幫助。您可能需要為非數(shù)字的數(shù)字字段的用戶(hù)輸入設(shè)計(jì)錯(cuò)誤處理,并運(yùn)行一些測(cè)試以查看missingSide用戶(hù)輸入可能出現(xiàn)的問(wèn)題并捕獲這些問(wèn)題。您可能想要嘗試的一種方法是給它一個(gè) 180 度的角度。某些值可能不會(huì)產(chǎn)生實(shí)際錯(cuò)誤,但可能會(huì)產(chǎn)生意想不到的結(jié)果。
祝你好運(yùn)!
添加回答
舉報(bào)