0

Im trying to run some python code in my terminal in vs code but i get errors when trying to run it. below is my code.

import string
from random import choice, shuffle

def randomPass():
    lower = list(string.ascli_lowercase)
    upper = list(string.ascli_uppercase)
    digits = list(string.digits)

    all = lower + upper + digits
    shuffle(all)

    length = int(input("How long is the password?: "))
    password = ''

    for i in range(length):
        password += choice(all)
        print(password)

    randomPass()

below is the errors i get when trying to run the code by using randomPass and How long is the password?: 10

terminal errors

4
  • 2
    Please note that it is ascii not ascli (line 5 and 6), also you need to remove the indentation for the last line. Commented Aug 13, 2022 at 0:16
  • 2
    FYI all is an inbuilt function in Python, try a different name. Commented Aug 13, 2022 at 0:16
  • This code has obvious errors. As posted, it will not run. Please post your real code. Commented Aug 13, 2022 at 0:20
  • Please post the error as text in a <pre></pre> tag, not as an image hosted on an external site. Commented Aug 13, 2022 at 0:44

1 Answer 1

1

Be careful with an indentation in your scripts:

import string
from random import choice, shuffle


def randomPass():
    lower = list(string.ascii_lowercase)
    upper = list(string.ascii_uppercase)
    digits = list(string.digits)

    all = lower + upper + digits
    shuffle(all)

    length = int(input("How long is the password?: "))
    password = ""

    for i in range(length):
        password += choice(all)
    print(password)


randomPass()

Also if we refactor this:

import string
from random import choice


def randomPass():
    lowercase = string.ascii_lowercase
    uppercase = string.ascii_uppercase
    digits = string.digits
    charset = lowercase + uppercase + digits

    length = int(input("How long is the password?: "))
    password = "".join(choice(charset) for _ in range(length))
    print(password)


randomPass()
Sign up to request clarification or add additional context in comments.

6 Comments

How do i know where lines need to be indented to im sorry im new to python. thank you all for the help.
@M1guelp, you know where lines need to be indented by reading and understanding either the Python language specification or SO's markdown specification. Please understand that indentation is how Python defines blocks of code, it is paramount.
@MichaelRuth got it, ill look over some documentation. Didn't know it was important thank you for the help.
With each tab you increase the nesting level of the code, the more to the right it is, the more nested it is. If you want your randomPass method to contain code - write it 1 tab after randomPass. In this example, randomPass has no indent, so one tab is required (0 tabs + 1 tab) for its code. In the randomPass function, you have a loop that has code in it, so it's written 1 tab after the loop (1 tab for loop + 1 tab for its contents), and so on. randomPass() is written without tabs, because it is not nested anywhere, it sits by itself and is on the same nesting level as randomPass.
In your example, print(password) was in a for loop, so it fired every time the loop iterated, this line was directly in the body of the loop, and not to the randomPass function, so I removed one tab to return this line in the randomPass function so it be called after the loop. Next, the randomPass() line you had one tab after randomPass, i.e. was in the randomPass function directly, so your code did nothing.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.