|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
题目:
创建变量 q20_amount 并为其分配一个数字。
编写一个 Python 程序来计算为一个人找零所需的最少纸币/硬币数量。
为了问题简单起见,假设我们只有纸币$20、$10、$5、硬币$2、$1。例如您需要找零$91,则需要四个$20、一个$10和一个$1。
该程序的输出应采用以下格式(对于上述示例$91) :
4 twenties
1 ten
1 one
使用条件语句来处理上述输出的单数和复数版本。当只需要一张账单时,您应该使用该词的单数版本。当需要多于一张票据时,应使用标签的复数形式。图1 提到的是该法案的单数和复数版本。
使用变量 output20 来分配解决方案。
提示:
要确定所需钞票的数量,您需要使用整数除法和模运算。
使用条件语句中的中间变量q20_20, q20_10, q20_5,q20_2和q20_1来决定 output20 中的单复数形式。
您可能需要在output2中使用strip()函数,删除额外 '\n' 的内容。
我的代码:
q20_amount = int(input('Enter a number:'))
q20_amount = 91
if q20_amount//20 == 1:
q20_20 = f'{q20_amount//20} twenty'
if q20_amount//20 > 1:
q20_20 = f'{q20_amount//20} twenties'
if q20_amount//20 < 1:
q20_20 = ''
if (q20_amount%20)//10 == 1:
q20_10 = f'{(q20_amount%20)//10} ten'
if (q20_amount%20)//10 > 1:
q20_10 = f'{(q20_amount%20)//10} tens'
if (q20_amount%20)//10 < 1:
q20_10 = ''
if (q20_amount%10)//5 == 1:
q20_5 = f'{(q20_amount%10)//2} five'
if (q20_amount%10)//5 > 1:
q20_5 = f'{(q20_amount%10)//2} fives'
if (q20_amount%10)//5 < 1:
q20_5 = ''
if (q20_amount%5)//2 > 1:
q20_2 = f'{(q20_amount%5)//2} twos'
if (q20_amount%5)//2 == 1:
q20_2 = f'{(q20_amount%5)//2} two'
if (q20_amount%5)//2 < 1:
q20_2 = ''
if q20_amount%2 == 1:
q20_1 = f'{q20_amount%2} one'
if q20_amount%2 > 1:
q20_1 = f'{q20_amount%2} ones'
if q20_amount%2 < 1:
q20_1 = ''
output20 = q20_20+q20_10+q20_5+q20_2+q20_1
print(output20)
我的输出结果:
Enter a number:91
4 twenties1 ten1 one
请问我敲的代码还有救吗?我真的不知道怎么修改了。大佬们的答案都很精辟,但我想找到我问题的原因,非常感谢! |
-
图1
|