gpt4 book ai didi

python - 在 python 中编写(for 和 while)循环程序?

转载 作者:行者123 更新时间:2023-11-28 21:49:47 25 4
gpt4 key购买 nike

我必须用 python 编写一个程序来解决这个问题:

Ask the user for an initial balance for their holiday club savings account. The user is to be prompted for 11 deposit amounts. Output the final amount in the account.

这就是我到目前为止所拥有的,但我迷路了。我不知道要添加什么才能获得最终金额。我也不知道如何计算 11 笔存款的数字。

balance = int(input("Enter initial balance: $"))
count = 1
numDep = 11
finalAmount = balance+numDep
while count <= 11:
n = int(input("Enter deposit #:")) # this needs to show deposits numbers up to 11
count += 1

print("Original price: $", balance)
print("Final Amount: $", finalAmount)

这就是我使用 while-loop 编写程序的全部内容。我仍然需要使用 for-loop 来编写它。

最佳答案

您需要求出总数。

balance = int(input("Enter initial balance: $ "))
count = 1
total =0
while count <= 11:
n = int(input("Enter deposit %s #: " %(count)))
count += 1
total += n #keep on adding deposit amounts.


print("Original price: $ %d" %balance)
print("Final Amount: $ %d" %(total + balance) )

使用 For 循环:

balance = int(input("Enter initial balance: $ "))
for i in range(11):
n = int(input("Enter deposit #: %s " %(i)))
total +=n

print("Original price: $ %d" %balance)
print("Final Amount: $ %d" %(total + balance) )

关于python - 在 python 中编写(for 和 while)循环程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32983419/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com