2 回答

TA貢獻1828條經驗 獲得超3個贊
無需函數即可完成此操作,否則您需要處理返回值。還可以使用 while 而不是 if 來使其更加健壯:
feet1 = int(input('Enter the Feet: '))
inches1 = int(input('Enter the Inches: '))
feet2 = int(input('Enter the Feet: '))
inches2 = int(input('Enter the Inches: '))
feet_sum = (feet1 + feet2)
inches_sum = (inches1 + inches2)
while (inches_sum) > 12:
inches_sum -= 12
feet_sum += 1
print('Feet: {} Inches: {}'.format(feet_sum, inches_sum))
另外,負數不會被處理,留給你作為練習:)
一切正常后,您可以嘗試將其提取為史蒂夫的答案中的函數。

TA貢獻1852條經驗 獲得超1個贊
我想這就是你想要的:
feet1 = int(input('Enter the Feet: '))
inches1 = int(input('Enter the Inches: '))
feet2 = int(input('Enter the Feet: '))
inches2 = int(input('Enter the Inches: '))
feet_sum = (feet1 + feet2)
inches_sum = (inches1 + inches2)
def check(inches_sum, feet_sum):
while (inches_sum) >= 12:
inches_sum -= 12
feet_sum += 1
return inches_sum, feet_sum
inches_sum, feet_sum = check(inches_sum, feet_sum)
print('Feet: {} Inches: {}'.format(feet_sum, inches_sum))
結果:
Enter the Feet: 1
Enter the Inches: 26
Enter the Feet: 1
Enter the Inches: 26
Feet: 6 Inches: 4
添加回答
舉報