[英]writing (for and while) loop program in python?
我必須在python中編寫有關此問題的程序:
向用戶詢問其假日俱樂部儲蓄帳戶的初始余額。 將提示用戶輸入11個存款金額。 在帳戶中輸出最終金額。
這是我到目前為止所擁有的,我迷路了。 我不知道要增加多少才能得到最終的金額。 我也不知道如何顯示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
編寫程序所需要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) )
在這里,它使用了for循環 。
balance = int(input("Enter the initial balance"))
beginning = balance
for i in range(1,11):
n = int(input(("Deposit"+str(i))))
balance += n
print("You had", beginning, "at the beginning")
print("Now, you have", balance)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.