鱼C论坛

 找回密码
 立即注册
查看: 1657|回复: 3

修改我的代码

[复制链接]
发表于 2023-8-1 17:41:29 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

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

图1
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-8-1 17:41:53 | 显示全部楼层
你的代码已经接近正确,只需要进行一些小的修改即可。主要问题在于变量的赋值和条件判断上有一些错误。

首先,我们不需要在代码中重新赋值  q20_amount = 91 ,因为你在输入语句中已经获取了用户输入的值。

其次,变量  q20_5  和  q20_1  的计算中存在问题,应该使用整除  //  而不是模运算  % 。此外,对于  q20_5  的计算,将  //2  修改为  //5 ,因为需要计算需要的 $5 纸币的数量。

最后,在输出结果中,每个项之间需要添加空格,否则结果会连在一起。

以下是修改后的代码:
q20_amount = int(input('请输入一个数字:'))

if q20_amount//20 == 1:
    q20_20 = f'{q20_amount//20} twenties '
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)//5} five '
if (q20_amount%10)//5 > 1:
    q20_5 = f'{(q20_amount%10)//5} 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)

现在,你的代码应该可以正确地输出结果了。当输入  91  时,输出应该是:
4 twenties 1 ten 1 one

这样输出的结果也符合题目要求的格式。希望这个解答对你有帮助!如果还有其他问题,请随时向我询问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-8-1 17:50:07 | 显示全部楼层
不用担心,您的代码是可以修正的。问题在于条件语句的逻辑和字符串拼接的方式。我将帮助您解释并纠正您的代码。

首先,看一下您的代码:
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)
问题在于您在计算需要的纸币数量时,有些地方使用了错误的变量和计算方式。同时,在拼接字符串时,也需要注意一些问题。

我将修正您的代码如下:
q20_amount = int(input('Enter a number:'))
# q20_amount = 91  # 这行代码可以注释掉或删除,因为上面已经有输入语句了。

q20_20 = q20_amount // 20
q20_10 = (q20_amount % 20) // 10
q20_5 = (q20_amount % 10) // 5
q20_2 = ((q20_amount % 10) % 5) // 2
q20_1 = ((q20_amount % 10) % 5) % 2

output20 = ""
if q20_20 == 1:
    output20 += f'{q20_20} twenty\n'
elif q20_20 > 1:
    output20 += f'{q20_20} twenties\n'

if q20_10 == 1:
    output20 += f'{q20_10} ten\n'
elif q20_10 > 1:
    output20 += f'{q20_10} tens\n'

if q20_5 == 1:
    output20 += f'{q20_5} five\n'
elif q20_5 > 1:
    output20 += f'{q20_5} fives\n'

if q20_2 == 1:
    output20 += f'{q20_2} two\n'
elif q20_2 > 1:
    output20 += f'{q20_2} twos\n'

if q20_1 == 1:
    output20 += f'{q20_1} one\n'
elif q20_1 > 1:
    output20 += f'{q20_1} ones\n'

print(output20.strip())
这样修改后,您应该能得到正确的输出。您可以根据您输入的不同数字进行测试,输出的格式应该是正确的。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-8-1 17:51:50 | 显示全部楼层
q20_amount = int(input('Enter a number:'))

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)//5} five'
if (q20_amount%10)//5 > 1:
    q20_5 = f'{(q20_amount%10)//5} 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
output20 = output20.strip()

print(output20)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-22 05:35

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表