#๐Ÿ”’ Default Directory in Visual Studio Code is always wrong

210 messages ยท Page 1 of 1 (latest)

true wigeon
#

I save all my files to a Python_Practice Directory. But whenever I open Visual Studio code and run my files, it always runs from a different directory

stiff vectorBOT
#

@true wigeon

Python help channel opened

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.

minor forge
#

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.

true wigeon
#

yea

#

idk why it keeps doing that

#

like i hate having to go and type cd ../{directory} everytime

hidden vale
#

i usually dont have to cd when i do that

true wigeon
#

Daggger!!!

#

ok

#

lemme see

#

that sorta works yea

#

it launches in the root dir

hidden vale
#

i would have a new folder fo each project and open said folder in vsc

true wigeon
#

well i opened the file itself in vsc

#

but it wont give an option to open the folder in vsc

hidden vale
#

ye that works but it doesnt set directory like u just encountered which is kinda dumb if u ask me

true wigeon
#

right

#

idk why thats even an issue

true wigeon
#

ohhh

#

yeppp

#

that worksss

#

thank god you werre awake

hidden vale
true wigeon
#

i am also stuck on some code, but ill try to work it out and post again

hidden vale
true wigeon
#

hmm ok lemme show you then

hidden vale
true wigeon
#

nah cowboy bebop

hidden vale
#

aw

#

was this close

true wigeon
#

its such a good anime

#

its double `` right

hidden vale
true wigeon
#

yea

hidden vale
#

!code

stiff vectorBOT
#
Formatting code on Discord

Here's how to format Python code on Discord:

```py
print('Hello world!')
```

These are backticks, not quotes. Check this out if you can't find the backtick key.

For long code samples, you can use our pastebin.

true wigeon
#

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))
hidden vale
#

oh god recursion

true wigeon
#

im trying to let the function print any list passed and add a "and, " after the second to last word

hidden vale
#

and u must use recursion ?

true wigeon
#

whats that

hidden vale
#

nvm u not using recursion im just stoopid

#

but tl;dr its a function that calls itself

true wigeon
#

oh right

true wigeon
hidden vale
#

!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))
stiff vectorBOT
# hidden vale !e ```py word = ['1', '2', '3', '4'] def printList(words): if len(words) >= ...

: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'
true wigeon
#

yea thats my error

hidden vale
#

this is recursion

true wigeon
#

oh ok

hidden vale
true wigeon
#

didnt know thats what that was

#

sorta

hidden vale
#

welp its complaining that words_copy doesnt have a insert attribute right

true wigeon
#

right

hidden vale
#

so lets look at what we made words_copy equal to

#

see if we spot any issues there

#

since its supposed to have one

true wigeon
#

we made it equal to a copy of words

#

so do i call words.insert instead?

hidden vale
#

we can

#

but lets look at the line

#

words_copy = words.copy

#

what is list.copy

true wigeon
#

method that copys the list

hidden vale
#

since its a method

#

what we need to do

true wigeon
#

()

#

now the problem is that is prints as a list and not a string

#

so i have to use str() somewhere

hidden vale
#

thats a fun line of thought and all but try giving it less than 2 elements

true wigeon
#

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)
hidden vale
#

!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)
stiff vectorBOT
# hidden vale !e ```py word = ['1', '2'] def printList(words): if len(words) > 2: ...

: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
true wigeon
#

wait

#

but its a copy of words

#

so it should have a value

hidden vale
#

but hint if u want: ||when is words_copy defined ?||

true wigeon
#

ok so fixed it

#

promise it didnt lool

#

but now its making it a string and not the list

true wigeon
#

i tried placing the str() in the print function

#

and then the return function

#

yet nothing

hidden vale
#

well what u think happens when u str(list)

true wigeon
#

makes the list into a string right?

hidden vale
#

well how exactly it does it

true wigeon
#

just like int() it turns the input into a interger output

hidden vale
stiff vectorBOT
true wigeon
#

so do i have to make a function to iterate through and print

hidden vale
#

try it

#

there is several ways to do this

true wigeon
#
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

gaunt sigil
#
>>> print(["1", "2", "and", "3"])
['1', '2', 'and', '3']
>>> print(*["1", "2", "and", "3"])
1 2 and 3
true wigeon
#

hmmm

#

havent learned that

#

lemme see

gaunt sigil
#

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

true wigeon
#

well its a function that takes place first

gaunt sigil
#

yeah but the function returns a list right?

true wigeon
#

right

gaunt sigil
#

return words_copy, which is a list

true wigeon
#

right

#

but when i use the *word in the call of the function, it says it only takes one argument

gaunt sigil
#

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)

true wigeon
#

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)
gaunt sigil
#

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'

gaunt sigil
#

i can expand on that if you don't know what to do with that next

gaunt sigil
#

yeaah not optimal

#

my third solution is probably the best

#

(', '.join(list[:-1]) + 'and') if len(list) > 1 else "" + list[-1] if list else ""

true wigeon
#

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

gaunt sigil
#

if it works, it works

true wigeon
#

yea its just fully understanding it now

#

readin ATBS but sometimes it can be tricky

gaunt sigil
true wigeon
#

please. I'd love to see as many options to know what works/ is easier

gaunt sigil
#

though it prints "1, 2, 3 and 4" and "1 and 2" instead of yours: "1, 2"

graceful forge
gaunt sigil
#

no need for len()

#
>>> [1,2,3,4,5][:-1]
[1, 2, 3, 4]
>>> [1,2,3,4,5][-1:]
[5]
true wigeon
#

okkk i see where you're going

graceful forge
#

need to skip 4 aswell

gaunt sigil
#

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)
graceful forge
#

btw wy are u doin this

true wigeon
#

reading ATBS and trying to learn python from scratch

#

one of the short projects in the book

graceful forge
#

ok

true wigeon
#

its more on the best practices side

gaunt sigil
#

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

graceful forge
gaunt sigil
#

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)
true wigeon
#

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

gaunt sigil
#

hmm

true wigeon
#

i also use codewars

gaunt sigil
#

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

true wigeon
#

howd you make the bots

#

and how far did you learn of python at that point

gaunt sigil
#

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

true wigeon
#

that sounds actually entertaining

#

how long would you say it took you to understand python as well as you do now

gaunt sigil
#

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

true wigeon
#

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

gaunt sigil
#

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__())

true wigeon
#

ill definitly take a look at these

#

i appreciate the help and insight

stiff vectorBOT
#
Python help channel closed for inactivity

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.