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.
Using subtraction on variables
Rasberry Pi 4B 2GB
Re: Using subtraction on variables
Is this what you want ?
You can run Python interactively (as here) to experiment and learn.
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
>>>
Pi4 8GB and Pi4 4GB running Raspberry Pi OS 64-bit
Re: Using subtraction on variables
Yes, but if i use -= it raises syntax error and points to = signjahboater wrote: ↑Thu Oct 01, 2020 7:34 pmIs this what you want ?You can run Python interactively (as here) to experiment and learn.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 >>>
Rasberry Pi 4B 2GB
Re: Using subtraction on variables
Really?
Try
x = x - 5
instead
Pi4 8GB and Pi4 4GB running Raspberry Pi OS 64-bit
Re: Using subtraction on variables
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
Re: Using subtraction on variables
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
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
Pi Interests: Home Automation, IOT, Python and Tkinter
Re: Using subtraction on variables
That approach might just work
, thank you for the idea. Will post after implementing.

Rasberry Pi 4B 2GB