Ask coding questions

← Back to all posts
How do I add to variables in python?
mhs_mhs123 (14)

Hey I am a new python developer and coding is just a new hobby of mine.

I was trying to make a butcher shop demo, where user can pick what kind of meat they want and pick how much (weight) of that type of meat and the program will keep track of how many kilograms of meat they bought as well as what kind of meat. At the very end, I'm planning to make a receipt for them which states what meats and prices. I got started with the project and I cam across a problem where I need to add to a variable to calculate the cost of the user. Any help with this would be appreciated! Thanks

https://repl.it/@mhs_mhs123/Butcher-Shop-Demo

Comments
hotnewtop
rediar (507)

@mhs_mhs123 It’s really easy! Unlike JavaScript you don’t need to do var variablename=1
Just do variablename=integer, float, string... etc.
You can globalize a variable too! global variablename which will make it so that you don’t need to constantly use int(), str() .
Here’s a link to help:https://www.learnpython.org/en/Variables_and_Types
please upvote or mark as answered if this helped!

lyscodingblox12 (3)

when i was doing a project for a coding class i was trying to code the program to say grade=4th grade in variables but it says that grade isnt a good word for print and i am new to coding so can you make it easy?

pattmax (3)

cost_per_pound = int(x)
total_cost = int(input("How many pounds do you need?")) * cost_per_pound

If you want cents consider using floats instead of integers. I'd recommend taking the Python 3 course on sololearn.

Edit: After looking at your code, you'd be best off making a food class with a name and cost attribute. You could remove some repeated code this way.

ebest (663)

Easy.
'totalcost = 1'

And for the receipt, you might want to add a list.

'foods = []'

Steven_The_GuyT (303)

If you need to add a number variable, just do

a = 5

Just change the number to whatever you need.

Geocube101 (623)

If you're trying to calculate cost given the amount per kilogram and the amount entered then this might work

amtofmeat = float(input('Amount of Meat (kg) ')) #Get Amount of Meat from Console
cost = amtofmeat * costperkilo #Multiply Amount Wanted by Amount per Kilo