Skip to content
Sign upLog in
← Back to Community

How do I add to variables in python?

Profile icon

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

Profile icon
Profile icon
Profile icon
Profile icon
Profile icon
Profile icon
Profile icon

@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:
please upvote or mark as answered if this helped!

Profile icon

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?

Profile icon

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.

Profile icon

'totalcost = 1'

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

'foods = []'

Profile icon

If you need to add a number variable, just do

a = 5

Just change the number to whatever you need.

Profile icon

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