如果詢問calculate width of a rectangle given perimeter and area
,我將e1和e2代碼部分作爲google方程的兩半,並將其代碼定爲兩半。計算給定面積和周長的矩形的寬度
這段代碼被設置爲一個較大塊的一部分,它以可視化的形式顯示計算的矩形而不是整數,但是當我測試它時,它給出的答案是不正確的。
import math
print("Welcome to Rectangles! Please dont use decimals!")
area = int(input("What is the area? "))
perim = int(input("What is the perimeter? "))
e1 = int((perim/4) + .25)
e2 = int(perim**2 - (16 * area))
e3 = int(math.sqrt(e2))
width = int(e1 * e3)
print(width)
你可以給一個輸入和預期的輸出 –
@KalyanReddy伊夫更新了帖子,澄清了一點,但說ID輸入100作爲區域和40則返回0外圍進入12的區域, 16的周長將返回32. –
您將周長除以4 ...因此,4個相等的邊是正方形。因此任何一方都是寬度。爲什麼'.25'確實爲你做?爲什麼會有一個隨機的'16'? –