我正在努力弄清楚为什么我的代码不断输出错误的值
I am struggling to figure out why my code keeps outputting the wrong values
在我当前的 zybooks 实验室 3.13 中,作业要求“编写一个程序,将零钱总金额作为整数输入,并使用最少的硬币输出零钱,每行一种硬币类型。硬币类型是美元、25 美分、Dimes、Nickels 和 Pennies。适当使用单数和复数硬币名称,例如 1 Penny 与 2 Pennies。
当我提交我的作品时,我收到了 2 项要求的“0/2”。如果整数中有“0”,我需要弄清楚如何让它停止输出 'No change' 。 HALP!!!!!
这是我的代码:
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
将减少为零。
您想要做的是在开始修改用户输入之前测试“无变化”情况,或者根据所有您积累的各种 dollars
、dimes
等值。
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
在我当前的 zybooks 实验室 3.13 中,作业要求“编写一个程序,将零钱总金额作为整数输入,并使用最少的硬币输出零钱,每行一种硬币类型。硬币类型是美元、25 美分、Dimes、Nickels 和 Pennies。适当使用单数和复数硬币名称,例如 1 Penny 与 2 Pennies。
当我提交我的作品时,我收到了 2 项要求的“0/2”。如果整数中有“0”,我需要弄清楚如何让它停止输出 'No change' 。 HALP!!!!!
这是我的代码:
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
将减少为零。
您想要做的是在开始修改用户输入之前测试“无变化”情况,或者根据所有您积累的各种 dollars
、dimes
等值。
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