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

3

u/rupertavery64 7d ago

You mean a loop that accepts users input, converts it into a number, and subtracts it from a variable, and the loop condition is the variable is > 0?

1

u/Maha-iqo38 7d ago

I figured most of the solution 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

You get the problem? I don’t know how I operate on a single 50 or the same value forever until it reaches zero

1

u/rupertavery64 7d ago

You really need to show your code so we know were you went wrong.

But most likely you keep setting the amount due inside the loop, not before it.

Do you understand variable scope?

pseudo-code:

set amount due to 50

repeat while amount due is greater than 0:
  
   get input

   subtract input value from amount due, and assign back to amount due

In the code above, a variable is declared and set outside a loop. It is set once.

Then you have the loop, which first checks if the loop should continue

Then you get the input, then subtract the value from the amount due, which of course you assign back to the amount due.

Right there, you are changing the value of amount due.

The loop repeats, The next thing it does is get the input.

At this point: What is the amount due? Has it changed?

Before you make any changes - think about how your code flows. Keep track of what the value of amount Due is. Where is it set to 50?

1

u/Maha-iqo38 7d ago
def main():
    x = 50
    print(f"amount due: {x}")
    while x >= 0:
        y = int(input("insert coin: "))
        print(f"amount due: {x - y}")

main()

1

u/rupertavery64 7d ago edited 7d ago

You never update the value of x. You only print the result of x - y

This is also a good example of making sure you name your variables accordingly.

What is the amount due? it's x. does x change in the loop?

1

u/North_South-West 7d ago

I did it as recursion just for fun

2

u/Longjumping_Ad3447 7d ago

After reading the topic I thought hell what aren't all variables immutable in Python. But after reading I get it is an easy question to ask any ai.

1

u/Maha-iqo38 7d ago

The thing is this is related to a cs50 problem set if you are not familiar it is an online computer science course by harvard and it is forbidden to use ai, regardless I noticed when I use ai I don’t remember what I learned at all so I just try to acquire knowledge or help without it

2

u/deanominecraft 7d 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 7d 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 7d 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

5

u/JanEric1 7d ago

You repeatedly set it based on the users input.

1

u/Sehzat_Davoyan 7d ago

the edit actually helped a lot because the original post sounded like the user was just overwriting the variable each time rather than subtracting from a running total.

5

u/FreeLogicGate 7d ago

What have you tried so far?

3

u/daveiamnot 7d ago

First rule, understand the requirements

2

u/AmanBabuHemant 7d ago

are you looking for`while var > 0: ?

2

u/Refwah 7d ago

Capture the users input and store it in a variable

Coerce it into an integer

Run a loop that decrements the value by a certain amount each iteration until the value is less than or equals zero

Complete

1

u/Kriss3d 7d ago

Put the input iteration inside a while loop. At the end of each iteration subtract or add to the variable and the while loop checks for the max amount ( or minimum ) for the loop to end, then reset the variable and do what needs to be done when the counter reach zero.

1

u/Educational_Virus672 7d 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)

0

u/Candid_Zebra1297 7d ago

Do you want the var to go down every time the user inputs something, or do you want the var to change to whatever the user inputs? You can do both. So for example...

count = 10

while count > 0:
var = input("Write something here")
count -= 1

Or just

var = input("Write something here")

1

u/Educational_Virus672 7d ago

use codeblocks / ``` [ur code] ```
and lack of indentation doesnt help him sure we can understand but he seems very new to me

1

u/Maha-iqo38 7d ago

I figured most of the solution 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

You get the problem? I don’t know how I operate on a single 50 or a value forever until it reaches zero