user_input = (input("Enter your purchase in dollars here: "))
updated_working_total = final_working_total - int(user_input)
print("You have", updated_working_total , "left")
I am working on a project where I collect a users monthly income, monthly rent/expenses, and create a weekly budget for them. After I collect the data I give them a working budget for the week. What I want is for the user to input the price of every purchase and subtract each purchase from the budget, hence:("updated_working_total"). My question is how can I repeat the block of code above so the user repeats this process and receives an updated budget everytime. Feel free to change my code as well. Thank you!
CodePudding user response:
Are you using a database
in your project? If you are using a database you can store each separate value user input. Then you can get the sum from the database and use the subtract operator to get the balance. I'm not sure you are using a user role base in your project. If you using a user role base you can do it also.
CodePudding user response:
Just wrap it into a function
def update_total(old_total):
new_purchase = int(input('Enter your purchase in dollars here:'))
updated_total = old_total - new_purchase
return updated_total
and then call it for example with a while loop that breaks at a certain input
total = 50 #initial total
while True:
total = update_total(total)
print(f'Your new remaining total is {total}')
if input('Do you want to enter another purchase?') != 'Yes':
break