我正在尝试编写一个程序,要求提供初始余额,费用减去。如果用户选择“是”以支付更多费用,则它会正确循环并要求另一个输入。如果用户选择N,程序将停止并打印“开始余额”,“费用总分录”和所有费用后的“余额”。以下代码正确地输出了所有内容,但在扣除所有费用后未输出正确的金额。
# Initialize the accumulator.
account_balance = 0
ending_balance = 0
expense = 0
keep_going = "y"
# Prompt user for account balance.
account_balance = float (input("Enter the starting balance of the account you want to use:"))
# Calculate the balance.
for a in range (1000):
while keep_going == "y":
# Prompt user for an expense.
purchase = int (input("What is the value of the expense?:"))
# Calculate the remaining balance.
ending_balance = account_balance - purchase
#Calculate the total expenses entered.
expense = expense + 1
# Ask if user has another expense.
keep_going = input("Do you have another expense? (Enter y for yes)")
if keep_going == "n":
print("Amount in account before expense subtraction $", format (account_balance, ",.2f"))
print("Number of expense entered:", expense)
print("Amount in account AFTER expenses subracted is $", format (ending_balance, ",.2f"))
# Psuedo Code
# Prompt user to enter ammount in account in which money will be withdran from`enter code here`
# Prompt user to enter amount of first expense
# Subtract expense from account
# Ask user if they would like to add another expense
# If "yes" then ask user to add expense
# If "no" then display the following
# Amount in account BEFORE expenses
# Number of transactions made
# Amount in account AFTER all expenses are subtracted
答案 0 :(得分:1)
如果您希望帐户余额为期初余额,则需要始终保留期初余额,而期末余额应为更新的余额。
# Initialize the accumulator.
account_balance = 0
ending_balance = 0
expense = 0
keep_going = "y"
# Prompt user for account balance.
account_balance = float (input("Enter the starting balance of the account you want to use:"))
# set ending balance to be the starting balance at the beginning
ending_balance = account_balance
# Calculate the balance.
for a in range (1000):
while keep_going == "y":
# Prompt user for an expense.
purchase = int (input("What is the value of the expense?:"))
# Update the ending balance instead
ending_balance = ending_balance - purchase
#Calculate the total expenses entered.
expense = expense + 1
# Ask if user has another expense.
keep_going = input("Do you have another expense? (Enter y for yes)")
if keep_going == "n":
print("Amount in account before expense subtraction $", format (account_balance, ",.2f"))
print("Number of expense entered:", expense)
print("Amount in account AFTER expenses subracted is $", format (ending_balance, ",.2f"))