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

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

我的腳本無法正常工作,但我相信代碼是正確的

我的腳本無法正常工作,但我相信代碼是正確的

烙印99 2023-08-22 10:12:10
我不明白為什么我的腳本不起作用!有人可以幫忙嗎?。?!我正在為我的計算機科學課做這個。這是代碼: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 True:        if (inches_sum) > 12:            inches_sum -= 12            feet_sum += 1            return feet_sum            return inches_sum            breakcheck(inches_sum, feet_sum)print('Feet: {} Inches: {}'.format(feet_sum, inches_sum))更新:這行得通嗎?我非常確定它應該在循環中獲取變量并檢查英寸是否超過 12,當英寸不超過 12 時,它將中斷循環。那有意義嗎?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, feet):    while True:        if (inches_sum) > 12:            inches_sum -= 12            feet_sum += 1        else:            breakcheck(inches_sum, feet_sum)print('Feet: {} Inches: {}'.format(feet_sum, inches_sum))
查看完整描述

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))

另外,負數不會被處理,留給你作為練習:)


一切正常后,您可以嘗試將其提取為史蒂夫的答案中的函數。


查看完整回答
反對 回復 2023-08-22
?
小怪獸愛吃肉

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


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

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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