Jump to content

python help! 2

Go to solution Solved by minibois,
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

infile = open("partslist.txt","r")
dinner = 0
lodge = 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()
 

 

 

can someone tell me why its telling me that totalLodging is not define? Thank you

Link to comment
https://linustechtips.com/topic/1049982-python-help-2/
Share on other sites

Link to post
Share on other sites

Because the first time you declare totalLodging you define it as itself. Or in other words, you're defining an undefined variable with an undefined variable.

 

totalLodging must be defined before it is used.

In laymens terms, you must assign a concrete value to totalLodging before you use it.

 

In programming, declaring means to say that something exists.

int myInteger; // this is a declaration. It sets aside memory to hold an integer and stores the handle in myInteger

 

defining means to give something a value:

myInteger = 0; // myInteger is now defined. That is, it has a value.

 

ENCRYPTION IS NOT A CRIME

Link to comment
https://linustechtips.com/topic/1049982-python-help-2/#findComment-12440687
Share on other sites

Link to post
Share on other sites

Please, please please, use the Code tags next time you post code:

https://linustechtips.com/main/announcement/12-please-use-code-tags/

 

Anyways, see what you're doing above with dinner, lodge and line?

dinner = 0
lodge = 0
line = infile.readline()

This is defining a variable; which you are not doing with totalLodging.

When you say:

totalLodging = totalLodging + amount

You're basically saying "add this non existing thing and amount together", python doesn't know totalLodging yet, so it doesn't know what you want to do with it.

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to comment
https://linustechtips.com/topic/1049982-python-help-2/#findComment-12440689
Share on other sites

Link to post
Share on other sites

Just now, straight_stewie said:

Because the first time you declare totalLodging you define it as itself. Or in other words, you're defining an undefined variable with an undefined variable.

 

totalLodging must be defined before it is used.

In laymens terms, you must assign a concrete value to totalLodging before you use it.

 

In programming, declaring means to say that something exists.


int myInteger; // this is a declaration. It sets aside memory to hold an integer and stores the handle in myInteger

 

defining means to give something a value:


myInteger = 0; // myInteger is now defined. That is, it has a value.

 

how come i didnt have problem with the totalDinner then? it skip it and told me the total lodging is wrong? 

Link to comment
https://linustechtips.com/topic/1049982-python-help-2/#findComment-12440690
Share on other sites

Link to post
Share on other sites

1 minute ago, Bryan 2760 said:

how come i didnt have problem with the totalDinner then? it skip it and told me the total lodging is wrong? 

That's an artifact of the lexical analysis algorithm that python uses. It just noticed totalLodging first. If you fix that, you will then get the same error for totalDinner.

ENCRYPTION IS NOT A CRIME

Link to comment
https://linustechtips.com/topic/1049982-python-help-2/#findComment-12440694
Share on other sites

Link to post
Share on other sites

27 minutes ago, Minibois said:

Please, please please, use the Code tags next time you post code:

https://linustechtips.com/main/announcement/12-please-use-code-tags/

 

Anyways, see what you're doing above with dinner, lodge and line?


dinner = 0
lodge = 0
line = infile.readline()

This is defining a variable; which you are not doing with totalLodging.

When you say:


totalLodging = totalLodging + amount

You're basically saying "add amount and this non existing thing together", python doesn't know totalLodging yet, so it doesn't know what you want to do with it.

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'

Link to comment
https://linustechtips.com/topic/1049982-python-help-2/#findComment-12440758
Share on other sites

Link to post
Share on other sites

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'

wait jk i think i fixed the totallodging now its giving me problems for totaldinner

Link to comment
https://linustechtips.com/topic/1049982-python-help-2/#findComment-12440764
Share on other sites

Link to post
Share on other sites

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

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to comment
https://linustechtips.com/topic/1049982-python-help-2/#findComment-12440776
Share on other sites

Link to post
Share on other sites

34 minutes ago, Minibois said:

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.

What I said above applies  to totalDinner I believe

omg thank you so much! it works :)

Link to comment
https://linustechtips.com/topic/1049982-python-help-2/#findComment-12440856
Share on other sites

Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now

×