#๐ Default Directory in Visual Studio Code is always wrong
210 messages ยท Page 1 of 1 (latest)
@true wigeon
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
I belive you can adjust this in VSC (not sure how, I'm not a VSC person, but you want the "working directory" setting for your project I think).
Yes, it's very annoying and the cause of many questions.
yea
idk why it keeps doing that
like i hate having to go and type cd ../{directory} everytime
try opening folder in vsc 
i usually dont have to cd when i do that
i would have a new folder fo each project and open said folder in vsc
well i opened the file itself in vsc
but it wont give an option to open the folder in vsc
ye that works but it doesnt set directory like u just encountered which is kinda dumb if u ask me
if it was in the middle of the day i prob wouldnt be, thank god u asked at midnight

i am also stuck on some code, but ill try to work it out and post again
u can just ask here, this place open for another hour
hmm ok lemme show you then
also i wanted to ask last time but forgot, is that lupin banner ? 
nah cowboy bebop
for code ?
yea
!code
rightttt
word = ['1', '2', '3', '4']
def printList(words):
if len(words) >= 2:
words_copy = words.copy
words_copy.insert(-1, 'and')
print(words_copy)
return words_copy
else:
print(words_copy)
return words_copy
print(printList(word))
oh god recursion
im trying to let the function print any list passed and add a "and, " after the second to last word
whats that
nvm u not using recursion im just stoopid
but tl;dr its a function that calls itself
oh right
welp u got p close
you mean the print(printList) right
!e
word = ['1', '2', '3', '4']
def printList(words):
if len(words) >= 2:
words_copy = words.copy
words_copy.insert(-1, 'and')
print(words_copy)
return words_copy
else:
print(words_copy)
return words_copy
print(printList(word))
:x: Your 3.12 eval job has completed with return code 1.
001 | Traceback (most recent call last):
002 | File "/home/main.py", line 12, in <module>
003 | print(printList(word))
004 | ^^^^^^^^^^^^^^^
005 | File "/home/main.py", line 5, in printList
006 | words_copy.insert(-1, 'and')
007 | ^^^^^^^^^^^^^^^^^
008 | AttributeError: 'builtin_function_or_method' object has no attribute 'insert'
yea thats my error
def func():
func()
this is recursion
oh ok
and u stuck on it ? 
welp its complaining that words_copy doesnt have a insert attribute right
right
so lets look at what we made words_copy equal to
see if we spot any issues there

since its supposed to have one
method that copys the list
()
now the problem is that is prints as a list and not a string
so i have to use str() somewhere
oof
yea it still prints 'and'
when its should only do it when its 3 or more
word = ['1', '2', '3', '4']
def printList(words):
if len(words) > 2:
words_copy = words.copy()
words_copy.insert(-1, 'and')
print(words_copy)
return words_copy
else:
print(words_copy)
return words_copy
printList(word)
!e
word = ['1', '2']
def printList(words):
if len(words) > 2:
words_copy = words.copy()
words_copy.insert(-1, 'and')
print(words_copy)
return words_copy
else:
print(words_copy)
return words_copy
printList(word)
:x: Your 3.12 eval job has completed with return code 1.
001 | Traceback (most recent call last):
002 | File "/home/main.py", line 12, in <module>
003 | printList(word)
004 | File "/home/main.py", line 9, in printList
005 | print(words_copy)
006 | ^^^^^^^^^^
007 | UnboundLocalError: cannot access local variable 'words_copy' where it is not associated with a value
i would stare at it for like 5min or so
but hint if u want: ||when is words_copy defined ?||
ok so fixed it
promise it didnt lool
but now its making it a string and not the list
yea figured
i tried placing the str() in the print function
and then the return function
yet nothing
makes the list into a string right?
well how exactly it does it
!e
print(str([1,2,3,4]))
:white_check_mark: Your 3.12 eval job has completed with return code 0.
[1, 2, 3, 4]
so do i have to make a function to iterate through and print
u can
try it

there is several ways to do this
word = ['1', '2']
def printList(words):
words_copy = words.copy()
for i in words_copy:
print(words_copy(i))
if len(words) > 2:
words_copy.insert(-1, 'and')
print(words_copy)
return words_copy
else:
print(words_copy)
return words_copy
printList(word)
word = ['1', '2']
def printList(words):
words_copy = words.copy()
for i in words_copy:
if len(words) > 2:
words_copy.insert(-1, 'and')
print(words_copy[i])
return str(words_copy)
else:
print(words_copy[i])
return str(words_copy[i])
printList(word)
so it tried that instead
gives error of indicies not being able to be str only slices
couldn't you put print(*word)
>>> print(["1", "2", "and", "3"])
['1', '2', 'and', '3']
>>> print(*["1", "2", "and", "3"])
1 2 and 3
essentially the * unwraps the arguments in the list
it turns it from this:
print( ["1", "2", "and", "3"] )
into this:
print( "1", "2", "and", "3" )
this only works if the function also lets you pass in as many arguments as are in the list
print() lets you do that
well its a function that takes place first
yeah but the function returns a list right?
right
return words_copy, which is a list
right
but when i use the *word in the call of the function, it says it only takes one argument
oops i missed your message
i think you misunderstood what i meant
Here's your function:
word = ['1', '2', '3', '4']
def printList(words):
if len(words) > 2:
words_copy = words.copy()
words_copy.insert(-1, 'and')
print(words_copy)
return words_copy
else:
print(words_copy)
return words_copy
printList(word)
You could change lines 6 and 9:
- print(words_copy)
+ print(*words_copy)
Also, since both branches of the if-statement end up running the same snippet of code eventually:py print(*words_copy) return words_copy You could take it out of the if-else-statement like so:```py
word = ['1', '2', '3', '4']
def printList(words):
words_copy = words.copy()
if len(words) > 2:
words_copy.insert(-1, 'and')
print(*words_copy)
return words_copy
printList(word)
(also your function will run into a NameError on line 9 when you try to print words_copy in the else branch, because you are trying to print words_copy, but it hasn't been defined yet: it has only been defined in the if len(words > 2): branch. You can either change the else branch to print(*words), or move words_copy = words.copy() one line higher, so it's outside of the if-statement block)
yea my bad, that code wasn't updated with the words_copy one line aboce
so your insight works
however
i need it to print ```py
1,2,3 and 4
word = ['1', '2', '3', '4']
def printList(words):
words_copy = words.copy()
if len(words) > 2:
words_copy.insert(-1, 'and')
print(*words_copy)
return words_copy
else:
print(*words_copy)
return words_copy
printList(word)
hmm true
you could add a comma to every element in the list, except for the last two, perhaps
or you could redo the function to iterate through the list and add a comma element between each until you reach the second last element in which case you add 'and'
you could split the function into two sections: [1,2,3] and [4]. Then print the first list with commas, and print the second list with an 'and' inbetween
i can expand on that if you don't know what to do with that next
not good
yeaah not optimal
my third solution is probably the best
(', '.join(list[:-1]) + 'and') if len(list) > 1 else "" + list[-1] if list else ""
so here's what i did. i did look online for some help but ```py
word = ['King', 'Jack', 'Chris', 'Kai']
def printList(words):
words_copy = words.copy()
if len(words) > 2:
words_copy.insert(-1, 'and')
output = ''
for i, item in enumerate(words_copy):
if item == 'and':
output += item + " "
elif i == len(words_copy) - 1:
output += item
else:
output += item + ', '
print(output)
return words_copy
else:
output = ', '.join(words_copy)
print(output)
return words_copy
printList(word)
i used the enumerate method
if it works, it works
i can write out a second method if you'd like me to
please. I'd love to see as many options to know what works/ is easier
though it prints "1, 2, 3 and 4" and "1 and 2" instead of yours: "1, 2"
maybe just do py part = len(words) - 1 print(*words_copy[:part],sep=",",end=" ") print(*words_copy[part:])
okkk i see where you're going
but still first part would 1,2,3,4,
need to skip 4 aswell
you can join the list together
or in your case the sep
word = ['King', 'Jack', 'Chris', 'Kai']
# word = ['Kai']
def printList(words):
first_section_words = words[:-1] # copy all elements except the last element. Also works if list is empty.
# ['King', 'Jack', 'Chris']
# []
first_section = ', '.join(first_section_words) # merge the first elements with a comma.
# "King, Jack, Chris"
# ""
if first_section: # if the first section is not empty.
first_section += " and "
# "King, Jack, Chris and "
# ""
if words: # list is not empty
print(first_section + words[-1])
# "King, Jack, Chris and Kai"
# "Kai"
printList(word)
btw wy are u doin this
reading ATBS and trying to learn python from scratch
one of the short projects in the book
ok
this seems easier to understand a little
its more on the best practices side
You can shorten the whole function like this (but not recommended for readability sake lol):
word = ['King', 'Jack', 'Chris', 'Kai']
def printList(words):
print(', '.join(words[:-1]) +
(" and " if words[:-1] else "") +
words[-1] if words else "")
printList(word)
a technical oneliner

oh wait, while [-1] returns an index error, [-1:] doesn't
so you can make the last line slightly less terrible
word = ['King', 'Jack', 'Chris', 'Kai']
def printList(words):
print(', '.join(words[:-1]) +
(" and " if words[:-1] else "") +
words[-1:])
printList(word)
yea readability is what i def wanna practice first before shortening
is there any other resources you guys mind sharing for learning python?
i use ATBS and this disc resources
hmm
i also use codewars
i learned most of my python through trial and error, using google (and very sometimes ai, but I only use that to speed up tasks i already know, not to learn new things)).
I started off with silly projects like the one you just had, learning how to manipulate elements in lists, or strings, to achieve a simple goal
Later, I started making a discord bot. That helped make my projects be slightly more useful to other people as well
but my first two bots were kind of meh until I finally got kind of a good starting point
i started off just looking at the examples of the Discord.py github repository
The easiest example is pretty much a copy paste
though setting up the bot on the discord developer portal may take a little bit of figuring out
after that, you learn how to read an incoming message, and then you can use your silly functions to change the words around, add commas between words.split(" "), and more.
An example is a command I made to look through the names of every server member, split it at capital letters, and make a list of names:
"John_Smith" -> "JohnSmith" -> {"John": 1, "Smith": 1}
And then count how often a name was present. And make a little leaderboard
that sounds actually entertaining
how long would you say it took you to understand python as well as you do now
well, i started learning python 7 years ago, so i would say it took 7 years to get here
but i started making my discord bot in 2022
during r/place 2022
ahhh ok
yea i just started fr like a month and a half ago so its been hard
but it makes sense it takes a while
but lately i've been working with different programming languages as well
I'd say C# helped understand classes, fields, properties, and methods a bit better since the python implementation of those is a bit wonky
but any more-popular programming language that isn't Python will likely give you a slightly better overview of those specifics.
C, C++, C#, Rust, Java
They all have more intuitive lambdas (inline functions) and properties, as well as logical operator functions: def *(a, b): return a * b (whereas python hacks its way around special symbols in functions using __mult__())
This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.