python help! 2
1 minute ago, Bryan 2760 said:infile = open("partslist.txt","r") totalDinner = 0 totalLodging = 0 line = infile.readline() while line != "": partslist = line.split(";") category = partslist[1] amount = partslist[2] if category == "dinner": totalDinner = totalDinner + amount if category == "Lodging": totalLodging = totalLodging + amount line = infile.readline() print ("Dinner:",totalDinner) print ("Lodging:",totalLodging) infile.close()ok so i did it like this and now its saying
Traceback (most recent call last):
File "C:/Users/Bryan/Desktop/hw 3 # 3.py", line 12, in <module>
totalLodging = totalLodging + amount
TypeError: unsupported operand type(s) for +: 'int' and 'str'
int = a number (with no decimals)
str = string = characters (like words, sentences, numbers, etc.)
python is saying you can't add the two together, because you can't add a number and a character together.
Depending on what you want to do (and what kind of character it is), you have two options:
1. "amount is a number, I want to add the two numbers together"
You have to convert amount to an int: https://guide.freecodecamp.org/python/how-to-convert-strings-into-integers-in-python/
Now they an be added together.
2. "amount is a character, I want to display these two words together."
you can do something like this:
totalDinner + "" + amount
This will basically tell python you dont want to add them together as a calculation; but rather just want to show these two separately as a string of characters.
3 minutes ago, Bryan 2760 said:wait jk i think i fixed the totallodging now its giving me problems for totaldinner
What I said above applies to totalDinner I believe

Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now