Happy
Happy

Reputation: 61

Adding multiple variables to get the sum?

I'm working on a small project regarding a game using Python and Discord webhooks. I'm wondering how to get the total ( sum ) of the variables that are put together? These variables have numbers listed that are grabbed from the page of the game site. Here's what I'm working with:

The variable I want to add the sum of are classed as robux and robux2.

Upvotes: 0

Views: 4947

Answers (1)

causation
causation

Reputation: 146

Ilja Everilä comment is correct. Assuming you split the text properly (i.e., the result of the split is a string of numbers) then you can cast the string into either an int or float to add them together. Below is a sample showing some strings being cast into numbers and then added together.

#Assuming the result of the split is a string of numbers

# If you expect integer scores
robux = '1'  
robux2 = '2'

total = int(robux) + int(robux2)
print(total) # prints -> 3


# If you expect float scores
robux = '1.1'
robux2 = '2.2'

total = float(robux) + float(robux2)
print(total) #prints -> 3.3

Before casting you should verify the that you split the text properly by printing the variables: print(robux) and print(robux2). Lastly, if you are planning on adding more variable to this code then you should also follow Ilja Everilä advice about using a list structure. Lists will let you do so much more with less code.

Upvotes: 1

Related Questions