razor23
Posts: 9
Joined: Wed Sep 23, 2020 6:02 am
Location: Croatia

Using subtraction on variables

Thu Oct 01, 2020 7:11 pm

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.
Rasberry Pi 4B 2GB

User avatar
jahboater
Posts: 6265
Joined: Wed Feb 04, 2015 6:38 pm
Location: Wonderful West Dorset

Re: Using subtraction on variables

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.
Pi4 8GB running PIOS64 Lite

razor23
Posts: 9
Joined: Wed Sep 23, 2020 6:02 am
Location: Croatia

Re: Using subtraction on variables

Thu Oct 01, 2020 7:41 pm

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
Rasberry Pi 4B 2GB

User avatar
jahboater
Posts: 6265
Joined: Wed Feb 04, 2015 6:38 pm
Location: Wonderful West Dorset

Re: Using subtraction on variables

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
Pi4 8GB running PIOS64 Lite

razor23
Posts: 9
Joined: Wed Sep 23, 2020 6:02 am
Location: Croatia

Re: Using subtraction on variables

Thu Oct 01, 2020 7:51 pm

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)
Rasberry Pi 4B 2GB

scotty101
Posts: 4097
Joined: Fri Jun 08, 2012 6:03 pm

Re: Using subtraction on variables

Fri Oct 02, 2020 8:59 am

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!")


Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

razor23
Posts: 9
Joined: Wed Sep 23, 2020 6:02 am
Location: Croatia

Re: Using subtraction on variables

Fri Oct 02, 2020 11:38 am

That approach might just work ;) , thank you for the idea. Will post after implementing.
Rasberry Pi 4B 2GB

Return to “Python”