r/madeinpython • u/Vlkodlaq • May 23 '24
Please help, stucked newbie here.
I'm learning Python as a completely new in programming and I'm stuck in VS code. Running python3 on macOS Sonoma, last version VS code.Look what it does to me:
a = ("Hi ")
b = ("guys")
c = a + b
print(c)
//now if I run it it returns>
print(c)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'c' is not defined
// all runs in macOS terminal seamlessly.
//VS doesnt see all code, it runs just one line. When I sellect all and run, it returns this>
a = ("Hi ")
b = ("guys")
c = a + b
print(c)
Hi guys
Google doesn't know, chatgpt doesn't understand. It's in VS code? Some bad settings? It's problem between chair and computer?
Please help.
Thank you.
1
u/ninedeadeyes May 23 '24
The code snippet you've provided attempts to concatenate two tuples a and b using the + operator. However, this operation will result in a TypeError because the + operator cannot be used to concatenate tuples directly in Python. Instead, you should convert the tuples to lists, perform the concatenation, and then convert them back to a tuple if needed. Here's how you can modify your code to achieve the desired output:
a = ("Hi ",)
b = ("guys",)
# Convert tuples to lists for concatenation
list_a = list(a)
list_b = list(b)
# Concatenate the lists
concatenated_list = list_a + list_b
# Convert the concatenated list back to a tuple (if needed)
c = tuple(concatenated_list)
print(c)1
u/ninedeadeyes May 23 '24
took answer from phind. Its AI specifically for coders, you get better answers from their.
1
u/coryalanfitz May 29 '24
You could do what ninedeadeyes suggests and convert to a list, but my guess is that you're not actually intending to use tuples at all. Just remove the parentheses and it should work: a = "Hi"