我需要编写一个程序使用FOR循环,要求用户7存款。当用户输入存款金额时,需要使用累加概念更新余额
此外,还应记录超过或等于1000美元、500至999美元、100至499美元以及0至99美元的存款数量。 输出=显示上述各组的计数以及所有存款的最终余额
问题:最后一个输入号码(存款)是唯一正在注册的号码
amount1000orover = 0
amount500to999 = 0
amount100to499 = 0
less99 = 0
total = 0
for count in range(7):
deposit = int(input("Please enter your deposit amount: "))
if deposit >= 1000:
amount1000orover + 1
total=total + deposit
if deposit>=500 and deposit<=999:
amount500to999 = amount500to999 + 1
total=total + deposit
if deposit>= 100 and deposit<=499:
amount100to499 = amount100to499 + 1
total=total + deposit
if deposit>=0 and deposit<=99:
less99 = less99 + 1
total=total + deposit
print("You have "+str(amount1000orover)+" deposit over or equal to 1000 dollars.")
print("You have "+str(amount500to999)+" deposit between 500 and 999 dollars.")
print("You have "+str(amount100to499)+" deposit between 100 and 499 dollars. ")
print("You have "+str(less99)+" deposit below 100 dollars.")
print("Your balance is : "+str(total))
请检查缩进,if循环应该在for循环内部,而不是外部。应该是这样的
在这种情况下,我建议使用elif语句来节省大量的输入。此外,由于这些钱被划分为不同的范围,你会损失特定的存款金额,尽管你仍然拥有总金额。解决问题的另一种方法是:
在当前版本的代码中,所有的
if
语句都在for
循环之外,这意味着它们将只针对最后一个用户输入(discount
)运行,因为这将是循环完成执行后持续存在的discount
值您可以通过一个示例看到这一点:
相反,您需要缩进
if
语句,使它们位于for count in range(7)
循环内。您还需要检查if
语句的内容,因为它们不会像您期望的那样递增amount1000orover
计数器。您可以使用+=
运算符来简化这些操作,即:然后,输出如您所期望的:
相关问题 更多 >
编程相关推荐