Page 1 of 1

Using subtraction on variables

Posted: Thu Oct 01, 2020 7:11 pm
by razor23
Hello

I have a list that gets filled with integers as a user press a button.

I want to make a default number like 10, and then tell program to substract from 10, number that user pressed, and next i want to substract from that substracted number, but all i seem to get is substracting from 10 all over again.
So if a user hits 5, i want that 5 i now default and if user press 3, i want the result to be 2, not 8.

How do i do this?

Thanks in andvance.

Re: Using subtraction on variables

Posted: Thu Oct 01, 2020 7:34 pm
by jahboater
Is this what you want ?

Code: Select all

Python 3.8.5 (default, Aug  1 2020, 10:14:08) 
[GCC 10.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> x = 10
>>> x -= 5
>>> x
5
>>> x -= 3
>>> x
2
>>> 
You can run Python interactively (as here) to experiment and learn.

Re: Using subtraction on variables

Posted: Thu Oct 01, 2020 7:41 pm
by razor23
jahboater wrote:
Thu Oct 01, 2020 7:34 pm
Is this what you want ?

Code: Select all

Python 3.8.5 (default, Aug  1 2020, 10:14:08) 
[GCC 10.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> x = 10
>>> x -= 5
>>> x
5
>>> x -= 3
>>> x
2
>>> 
You can run Python interactively (as here) to experiment and learn.
Yes, but if i use -= it raises syntax error and points to = sign

Re: Using subtraction on variables

Posted: Thu Oct 01, 2020 7:44 pm
by jahboater
razor23 wrote:
Thu Oct 01, 2020 7:41 pm
Yes, but if i use -= it raises syntax error and points to = sign
Really?
Try

x = x - 5

instead

Re: Using subtraction on variables

Posted: Thu Oct 01, 2020 7:51 pm
by razor23
jahboater wrote:
Thu Oct 01, 2020 7:44 pm
razor23 wrote:
Thu Oct 01, 2020 7:41 pm
Yes, but if i use -= it raises syntax error and points to = sign
Really?
Try

x = x - 5

instead
To tell you thruth, i have a list, that is filled with input numbers (darts game), and i want to display substractions of 501 or any other game when a user inputs (hits) number, it get stored in the list.

I know i can use last item in list by using list[-1], but i want it to substract from 501, and all i can do is substract from 501, i want that 501 becomes whatever the substracted item is (integer in this case)

Re: Using subtraction on variables

Posted: Fri Oct 02, 2020 8:59 am
by scotty101
razor, I'm sure that what you are trying to do would be a bit clearer if you shared an example of your code.

Taking the last item from a list and subtracting it from a number isn't difficult but you may just have things in the wrong sequence.

Rather than try to take the most recent number away from the current score, why not find the sum of all the numbers in the list and subtract this from 501. The sum function in python will make this easy.

I'm not a darts player but what about something like this

Code: Select all

TARGET = 501

score_list = []

while sum(score_list) < 501:
    score = 0
    remaining = TARGET - sum(score_list)        
    
    print(f"You have {remaining} left to score")
    while not score:
        try:
            score = int(input("Enter your score for this round: "))
        except ValueError:
            print("You must enter a valid number")

    if score == 180:
        print("ONE HUNDRED AND EIGHTY!!!")
    score_list.append(score)

print("Well done!")



Re: Using subtraction on variables

Posted: Fri Oct 02, 2020 11:38 am
by razor23
That approach might just work ;) , thank you for the idea. Will post after implementing.