簡體   English   中英

我正在努力弄清楚為什么我的代碼不斷輸出錯誤的值

[英]I am struggling to figure out why my code keeps outputting the wrong values

在我當前的 zybooks 實驗室 3.13 中,作業要求“編寫一個程序,總找零金額為 integer 輸入,零錢為 output,使用最少的硬幣,每行一種硬幣類型。硬幣類型是 Dollars, Quarters, Dimes, Nickels,和便士。適當使用單數和復數硬幣名稱,例如 1 便士與 2 便士。”

當我提交我的工作時,我收到 2 項要求的“0/2”。 如果 integer 中有一個“0”,我需要弄清楚如何讓它停止輸出“無變化”。HALP!!!!!

不良輸出 1不良輸出 2

這是我的代碼:

user_input = int(input())

# Conditional math Block: Used to determine user input value in amount of dollars, quarters, dimes, nickles and pennies.
dollar = user_input // 100  # convert to dollars
user_input %= 100  # remainder after conversion

quarter = user_input // 25  # convert to quarters
user_input %= 25  # remainder after conversion

dime = user_input // 10  # convert to dimes
user_input %= 10  # remainder after conversion

nickel = user_input // 5  # convert to nickels
user_input %= 5  # remainder after conversion

penny = user_input

# Determines if user input is invalid.
if user_input <= 0:
    print('No change')

# Condition Block: Analyzes user input and assigns appropriate values.
if dollar > 1:  # Uses math block to determine if user input is equivalent to more than 1 Dollar.
    print(dollar, 'Dollars')
elif dollar == 1:  # Uses math block to determine if user input is equivalent to 1 Dollar.
    print(dollar, 'Dollar')

if quarter > 1:  # Uses math block to determine if user input is equivalent to more than 1 Quarter.
    print(quarter, 'Quarters')
elif quarter == 1:  # Uses math block to determine if user input is equivalent to 1 Quarter.
    print(quarter, 'Quarter')

if dime > 1:   # Uses math block to determine if user input is equivalent to more than 1 Dime.
    print(dime, 'Dimes')
elif dime == 1:  # Uses math block to determine if user input is equivalent to 1 Dime.
    print(dime, 'Dime')

if nickel > 1:   # Uses math block to determine if user input is equivalent to more than 1 nickel.
    print(nickel, 'Nickels')
elif nickel == 1:  # Uses math block to determine if user input is equivalent to 1 nickel.
    print(nickel, 'Nickel')

if penny > 1:  # Uses math block to determine how many Pennies' user input is equivalent to.
    print(penny, 'Pennies')
elif penny == 1:  # Uses math block to determine how many Pennies' user input is equivalent to.
    print(penny, 'Penny') ```

您可以在計算前打印“No change”

user_input = int(input())
if user_input <= 0: 
    print('No change')
dollar = user_input//100
user_input %= 100
#the rest

問題的症結在於你這樣做:

if user_input <= 0:
    print('No change')

您已經減少user_input的值之后——例如,這一行:

user_input %= 10  # remainder after conversion

如果它是以 0 結尾的任何數字,會將user_input減少為零。

您要做的是在開始修改用戶輸入之前測試“無變化”情況,或者根據您積累的所有各種dollarsdimes等值對其進行測試。

FWIW,我建議首先不要將所有這些東西存儲在復制+粘貼變量中。 下面是一個示例,說明如何通過預先定義所有硬幣類型,然后使用每種硬幣的適當值重復操作來循環完成整個操作:

total = int(input())
if total <= 0:
    print("No change")
else:
    for value, s_name, p_name in [
        (100, "Dollar", "Dollars"),
        (25, "Quarter", "Quarters"),
        (10, "Dime", "Dimes"),
        (5, "Nickel", "Nickels"),
        (1, "Penny", "Pennies"),
    ]:
        qty = total // value
        if not qty:
            continue
        print(f"{qty} {p_name if qty > 1 else s_name}")
        total %= value

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM