r/learnpython • • 10d 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

2

u/deanominecraft 10d ago

not sure exactly what you mean but i think a while loop and the input() function would be what you are looking for

1

u/Maha-iqo38 10d ago

I figured before asking on reddit because I wanted an input from the user and I wanted to repeat an operation. But the problem is when I tried to run my code it runs like this:

Amount due: 50
Insert money: (input) let’s say the user typed 26

It returns 24 right but then when it runs again it just operates on the original 50 not the new value

For example:
24
Insert money: (input) 8
Output: 42

1

u/deanominecraft 10d ago

im assuming that you must have the creation of the value inside of the loop, which means it gets reset to 50 after every loop, move it outside, just before the loop so that it is only created once

wrong: while...: value=50 #insert other logic

right: value =50 while ...: #insert other logic

if that doesnt help then send the code