Home > Software design >  Simple python equation
Simple python equation

Time:11-09

I'm a new pyhton programmer, I'm writing a simple program that calculate what I have in my storage and what is remaining so let's assume that x = 1000 I want to deduct 200 items now and after a day I'll deduct 300 more the problem here that programme will deduct 200 out of the 1000 and deduct 300 out of the same 1000 not from remaining x how can I solve this problem?

x = 1000

While x !=0:`your text`
   b = int(input("how many (x) you need:)
   remaining = x - b
   Print(remaining)
else:
    pass

CodePudding user response:

You're never changing the origianl x variable

You need to take your input value minus x and feed it back into x

I've added some extra bits for you, for validation under 0 etc. x = 1000

while True:
    b = input("how many (x) you need:")
    
    #make sure its a number
    is_valid_input = b.isnumeric()
    
    #if it IS a valid number
    if is_valid_input: 
    
        #get the new value (your input minus the current x value)
        new_val = x - b

        #check here to see if under 0
        if new_val < 0:
            print("Cannot go below 0")
            #this exits the while loop
            break
        else:
            # if ok update the x value
            x = new_val
        
        print(f"value is now at {x}")
        
    #if its not a number    
    else:
        print("Please enter a valid number, not text")

CodePudding user response:

Like Lewis said, you're not changing the amount you have in storage (the x variable), so every time your user enters an amount, you're subtracting from x, which will remain at 1000 until you change it. Instead of creating the new remaining variable, you could use x -= b and then print x. He did a favor giving you validators, but if you just want your code to work, here's a simple solution.

x = 1000

While x !=0:`your text`
   b = int(input("how many (x) you need:)
   x -= b
   Print(x)
else:
   pass
  • Related