0

so, this is my script

import random
import string

alphabet = "abcdefghijklmnopqrstuvwxyz"

#How many letters
def let_count():
    letters_count = -1
    while letters_count > 26 or letters_count < 0:
        letters_count = int(input("How many letters?(max 26): "))
    letters_count = 26 - letters_count
    make_letter_list(letters_count)

#Make list
def make_letter_list(letters_count):
    letters = list(map(chr, range(97, 123-letters_count)))
    print("\n"+", ".join(letters))
    main(letters)


#Make lists of elements in letters
def mloeil(i, vvv, three_nums):
    if vvv:
        av = True
        bv = True
        cv = True
        dv = True
        ev = True
        fv = True
        gv = True
        hv = True
        iv = True
        jv = True
        vvv = False
    if i == "a":
        if av:
            a = []
            av = False
        return a.append(three_nums)
    elif i == "b":
        if bv:
            b = []
            bv = False
        return b.append(three_nums)
    elif i == "c":
        if cv:
            c = []
            cv = False
        return c.append(three_nums)
    elif i == "d":
        if dv:
            d = []
            dv = False
        return d.append(three_nums)
    elif i == "e":
        if ev:
            e = []
            ev = False
        return e.append(three_nums)

def main(letters):
    vvv = True
    for i in letters:
        print("\n"+i+")")
        num_count = 1
        for y in range(3):
            three_nums = int(input("Number"+str(num_count)+":"))
            mloeil(i, vvv, three_nums)
            num_count += 1

    print(a)

let_count()

and at the end where is "print(a)" it just says and error:

NameError: name 'a' is not defined

why is that ? i definied "a" in here:

if i == "a":
        if av:
            a = []
            av = False
        return a.append(three_nums)

any suggestions ? I know it is a bit messy, but i am still learning, i tried many things but none of them worked. Problems are "def mloeil" and "def main". I tried to return from "mloeil" just the letters, but did not work either. Thanks for your support

Jastcher
  • 3
  • 3
  • 1
    `a` is defined inside a function. It will not be available outside the function – rdas Mar 25 '20 at 22:35
  • how can I fix it then ? – Jastcher Mar 25 '20 at 22:36
  • You need to `return` values from your function if you need to use them in another function – G. Anderson Mar 25 '20 at 22:37
  • 1
    And you can't return `a.append(...)` - `append` modifies the list in-place – rdas Mar 25 '20 at 22:38
  • so append, and then "return a" below ?, doesnt work – Jastcher Mar 25 '20 at 22:43
  • This line inside main: mloeil(i, vvv, three_nums). Should be like this for print (a) to work: a=mloeil(i, vvv, three_nums) provided that you ( returned a ) from inside mloeil function. However , still there are some logical errors in your code. – Assem Mar 25 '20 at 22:51
  • What is the expected output? Knowing will help me significantly while troubleshooting – B-L Mar 25 '20 at 23:03
  • i want list of each element in list "letters", which will have 3 * "three_nums" inside, so example: a = [14, 57, 37], b = [78, 39, 69], c = [... – Jastcher Mar 25 '20 at 23:58

1 Answers1

0

a is defined inside the function mloeil, and so only exists inside that function; attempting to access a outside the function will result in an error.

If you want the value of a to be usable outside the function, then mloeil needs to return that value, and then you need to use it or store it somewhere.

Peritract
  • 753
  • 4
  • 13