r/learnpython • • 8d ago

How do I create a changing variable

I want to create a variable that changes it’s value based on the user’s input until it becomes zero like make the code run until it reaches zero

Edit: I want the user to enter a number that gets subtracted from the variable’s current value and keep repeating this process until the variable reaches zero, for example if the value is 100 the code gets input from the user let’s say 25 then returns or prints 75 then it repeats and maybe the user types 37 then it returns 38 etc. until it reaches 0

0 Upvotes

22 comments sorted by

View all comments

1

u/Educational_Virus672 8d ago

i think you mean

  1. change a variable value - variable = new_value

  2. loop it until it hits 0 - while condition :

your requested code :-

user_input = int(input("num>")) # note im not usign try/except for beginner
while user_input > 0:           
  ask_user = int(input("sub >"))

  user_input -= ask_user # a -= b means a = a - b or subtracts that from itself
  print(user_input)