通过 Singular/Plural 差异化做出改变
Making Change with Singular/Plural Differentiation
我正在编写脚本初学者 class,我们需要编写一些代码来根据整数输入进行更改。它还需要为每种硬币类型打印正确的复数。我在让我的代码工作时遇到了一些麻烦,因为它在它执行的第一个操作时停止,我不确定如何让它继续。我猜这与我如何布置所有这些 elif 语句有关,我不确定正确的做法是让代码继续,如果它仍然有剩余的变化:
# Get input for amount of change
change = int(input())
# If change is less than 1, print 'No change'
if change < 1:
print('No change')
# If change is over 199, subtract and print 'X Dollars'
elif change > 199:
print(change // 100, 'Dollars\n')
change = change % 100
# If change is 100-199, subtract and print '1 Dollar'
elif 99 < change <= 199:
print('1 Dollar\n')
change = change % 100
# If remaining change is 50-99, subtract and print 'X Quarters'
elif 49 < change <= 99:
print(change // 25, 'Quarters\n')
change = change % 25
# If remaining change is 25-49, subtract and print '1 Quarter'
elif 24 < change <= 49:
print('1 Quarter\n')
change = change % 25
# If remaining change is 25, print '1 Quarter' and set change to 0
elif change == 25:
print('1 Quarter')
change = 0
# If remaining change is 20-24, subtract and print '2 Dimes'
elif 19 < change <= 24:
print('2 Dimes\n')
change = change % 10
# If remaining change is 10, print '1 Dime' and set change to 0
elif change == 10:
print('1 Dime')
change = 0
# If remaining change is 5-9, subtract and print '1 Nickel'
elif 4 < change <= 9:
print('1 Nickel\n')
change = change % 5
# If remaining change is 2-4, subtract and print 'X Pennies'
elif 1 < change <= 4:
print(change // 1, 'Pennies')
change = change % 1
# If remaining change is 1, print '1 Penny' and set change to 0
elif change == 1:
print('1 Penny')
change = 0
谁能给我个主意?谢谢你的时间。
始终从其余更改中减去您已经匹配的内容。
然后继续使用 IF 而不是 ELIF,因为 elif 将永远不会再次匹配之前的 if 匹配。
# Get input for amount of change
change = int(input())
# If change is less than 1, print 'No change'
if change < 1:
print('No change')
# If change is over 199, subtract and print 'X Dollars'
if change > 199:
print(change // 100, 'Dollars\n')
change = change - change // 100 * 100
# If change is 100-199, subtract and print '1 Dollar'
if 99 < change <= 199:
print('1 Dollar\n')
change = change - 100
# If remaining change is 50-99, subtract and print 'X Quarters'
if 49 < change <= 99:
print(change // 25, 'Quarters\n')
change = change - change // 25 * 25
# ....
等等...
如果我答对了你的问题,那就是根据 'change' 打印 dollars
/dollar
[和子值],它会一直减少直到为零。
让我澄清一下(不是美元用户:):
- 1 便士 = 1 美分
- 1 镍 = 5 美分
- 1 角 = 10 美分
- 1 夸特 = 25 美分
- 1 美元 = 100 美分
因此,如果输入是 1451
美分,则预期输出是:1451 = 14 Dollars 2 Quarters 0 Dime 0 Nickel 1 Penny
你可以这样做:
change = int(input())
wt = {'Dollar': 0, 'Quarter': 0, 'Dime': 0, 'Nickel': 0, 'Penny': 0} # weight
wt['Dollar'] = change // 100
change = change % 100
wt['Quarter'] = change // 25
change = change % 25
wt['Dime'] = change // 10
change = change % 10
wt['Nickel'] = change // 5
change = change % 5
wt['Penny'] = change // 1
change = change % 1
for key, value in wt.items():
print(f'{value} {key}s' if value > 1 else f'{value} {key}', end=' ')
print()
输出:
➜ python file.py
112341
1123 Dollars 1 Quarter 1 Dime 1 Nickel 1 Penny
➜ python file.py
24
0 Dollar 0 Quarter 2 Dimes 0 Nickel 4 Pennys # you might wanna add some small condition to rectify this :)
➜ python file.py
521
5 Dollars 0 Quarter 2 Dimes 0 Nickel 1 Penny
我正在编写脚本初学者 class,我们需要编写一些代码来根据整数输入进行更改。它还需要为每种硬币类型打印正确的复数。我在让我的代码工作时遇到了一些麻烦,因为它在它执行的第一个操作时停止,我不确定如何让它继续。我猜这与我如何布置所有这些 elif 语句有关,我不确定正确的做法是让代码继续,如果它仍然有剩余的变化:
# Get input for amount of change
change = int(input())
# If change is less than 1, print 'No change'
if change < 1:
print('No change')
# If change is over 199, subtract and print 'X Dollars'
elif change > 199:
print(change // 100, 'Dollars\n')
change = change % 100
# If change is 100-199, subtract and print '1 Dollar'
elif 99 < change <= 199:
print('1 Dollar\n')
change = change % 100
# If remaining change is 50-99, subtract and print 'X Quarters'
elif 49 < change <= 99:
print(change // 25, 'Quarters\n')
change = change % 25
# If remaining change is 25-49, subtract and print '1 Quarter'
elif 24 < change <= 49:
print('1 Quarter\n')
change = change % 25
# If remaining change is 25, print '1 Quarter' and set change to 0
elif change == 25:
print('1 Quarter')
change = 0
# If remaining change is 20-24, subtract and print '2 Dimes'
elif 19 < change <= 24:
print('2 Dimes\n')
change = change % 10
# If remaining change is 10, print '1 Dime' and set change to 0
elif change == 10:
print('1 Dime')
change = 0
# If remaining change is 5-9, subtract and print '1 Nickel'
elif 4 < change <= 9:
print('1 Nickel\n')
change = change % 5
# If remaining change is 2-4, subtract and print 'X Pennies'
elif 1 < change <= 4:
print(change // 1, 'Pennies')
change = change % 1
# If remaining change is 1, print '1 Penny' and set change to 0
elif change == 1:
print('1 Penny')
change = 0
谁能给我个主意?谢谢你的时间。
始终从其余更改中减去您已经匹配的内容。 然后继续使用 IF 而不是 ELIF,因为 elif 将永远不会再次匹配之前的 if 匹配。
# Get input for amount of change
change = int(input())
# If change is less than 1, print 'No change'
if change < 1:
print('No change')
# If change is over 199, subtract and print 'X Dollars'
if change > 199:
print(change // 100, 'Dollars\n')
change = change - change // 100 * 100
# If change is 100-199, subtract and print '1 Dollar'
if 99 < change <= 199:
print('1 Dollar\n')
change = change - 100
# If remaining change is 50-99, subtract and print 'X Quarters'
if 49 < change <= 99:
print(change // 25, 'Quarters\n')
change = change - change // 25 * 25
# ....
等等...
如果我答对了你的问题,那就是根据 'change' 打印 dollars
/dollar
[和子值],它会一直减少直到为零。
让我澄清一下(不是美元用户:):
- 1 便士 = 1 美分
- 1 镍 = 5 美分
- 1 角 = 10 美分
- 1 夸特 = 25 美分
- 1 美元 = 100 美分
因此,如果输入是 1451
美分,则预期输出是:1451 = 14 Dollars 2 Quarters 0 Dime 0 Nickel 1 Penny
你可以这样做:
change = int(input())
wt = {'Dollar': 0, 'Quarter': 0, 'Dime': 0, 'Nickel': 0, 'Penny': 0} # weight
wt['Dollar'] = change // 100
change = change % 100
wt['Quarter'] = change // 25
change = change % 25
wt['Dime'] = change // 10
change = change % 10
wt['Nickel'] = change // 5
change = change % 5
wt['Penny'] = change // 1
change = change % 1
for key, value in wt.items():
print(f'{value} {key}s' if value > 1 else f'{value} {key}', end=' ')
print()
输出:
➜ python file.py
112341
1123 Dollars 1 Quarter 1 Dime 1 Nickel 1 Penny
➜ python file.py
24
0 Dollar 0 Quarter 2 Dimes 0 Nickel 4 Pennys # you might wanna add some small condition to rectify this :)
➜ python file.py
521
5 Dollars 0 Quarter 2 Dimes 0 Nickel 1 Penny