Python Forum
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Can't figure this one out
#1
I'm brand new to programming, busy practicing some coding. What am I doing wrong here? Confused 

>>>farm_equipment = ["jd r4038", "jd 9620", "jd 9630", "jd 4455", "jd 6140d", "jd 9860", "jd 9870", "jd seeder"]
>>>message_c = "\nList of current equipment: "
>>>print(message_c + farm_equipment)
I'm getting the following error:

Error:
Traceback (most recent call last):   File "every_function_c3.py", line 17, in <module>     print(message_c + farm_equipment) TypeError: must be str, not list
Of course there are other lines of code, they all execute perfectly, but I'm not sure if any of those have an impact on these particular lines.

Thanks in advance for any help!
Reply
#2
(May-14-2017, 07:39 PM)Stefython Wrote: I'm brand new to programming, busy practicing some coding. What am I doing wrong here? Confused 

>>>farm_equipment = ["jd r4038", "jd 9620", "jd 9630", "jd 4455", "jd 6140d", "jd 9860", "jd 9870", "jd seeder"]
>>>message_c = "\nList of current equipment: "
>>>print(message_c + farm_equipment)


I'm getting the following error:

Traceback (most recent call last):
  File "every_function_c3.py", line 17, in <module>
    print(message_c + farm_equipment)
TypeError: must be str, not list

You are trying to add incompatible types - string and list. You can print either directly - though in case of list it may look strange to you.

In order to concatenate, you have to convert list to string - one of the usual methods is to join it
    print(message_c + ', '.join(farm_equipment))
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#3
Farm equipment is a list of strings. You cant concatenate a string with a list.
Recommended Tutorials:
Reply
#4
Hello!
The + sign is string concatenation. But string concatenation, not string + list. They are different types.
Try:
print('{}{}'.format(message_c, farm_equipment))

It's the simplest way
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
Thanks everybody!
Your answers helped allot.

I'm just still a bit confused. In earlier lines of code I used the same type of code as described above and it would execute without any error:

message = "\nMy favorite machine is the "

print(message + farm_equipment[0].upper())
The result:

Output:
My favorite machine is the JD R4038
Is this because I'm specifying an element from the list, which is a string and is thus allowed?
Reply
#6
(May-14-2017, 08:59 PM)Stefython Wrote: s this because I'm specifying an element from the list, which is a string and is thus allowed?
The fact that it worked should be a good indictor that its allowed.what do you want it to do?
Recommended Tutorials:
Reply
#7
Well, you could use join to get some normal looks for the print function

print('{}{}'.format(message_c, ', '.join(farm_equipment)))
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020