0

Thanks firstly for bearing with me as a relative newcomer to the world of Python. I'm working on a simple set of code and have been racking my brain to understand where I am going wrong. I suspect it is a relatively simple thing to correct but all searches so far have been fruitless. If this has been covered before then please be gentle, I have looked for a couple of days!

I'm working on the following and after catching and correcting a number of issues I suspect that I'm on the last hurdle:-

def main():
    our_list = []
    ne = int(input('How many numbers do you wish to enter?  '))
    for i in range(0, (ne)): # set up loop to run user specified number of time
        number=int(input('Choose a number:-  '))
        our_list.append(number) # append to our_list
    print ('The list of numbers you have entered is ') 
    print (our_list) 
main()

while True:
    op = input ('For the mean type <1>, for the median type <2>, for the mode type <3>, to enter a new set of numbers type <4> or 5 to exit')
    import statistics
    if op == "1":
        mn = statistics.mean(our_list)
        print ("The mean of the values you have entered is:- ",mn)
    if op == "2":
        me = statistics.median(our_list)
        print ("The median of the values you have entered is:- ",me)
    if op == "3":
        mo = statistics.mode(our_list)
        print ("The mode of the values you have entered is:- ",mo)
    if op == "5":
        main()
    else:
        print("Goodbye")
        break`

For some reason the appended (our_list) is not being recognised within the while true loop rendering the statistics calculation void. Any steer would be really appreciated as to where I am missing the obvious, thanks in advance.

Cheers Bryan

1

3 Answers 3

1

I'm not sure exactly what you mean by "not being recognized", but our_list is a local variable inside main, so it can't be used anywhere but inside main.

So, if you try to use it elsewhere, you should get a NameError.

If your code actually has a global variable with the same name as the local variable that we aren't seeing here, things can be more confusing—you won't get a NameError, you'll get the value of the global variable, which isn't what you want.

The best solution here is to return the value from the function, and then have the caller use the returned value. For example:

def main():
    our_list = []
    ne = int(input('How many numbers do you wish to enter?  '))
    for i in range(0, (ne)): # set up loop to run user specified number of time
        number=int(input('Choose a number:-  '))
        our_list.append(number) # append to our_list
    print ('The list of numbers you have entered is ') 
    print (our_list) 
    return our_list

the_list = main()

while True:
    op = input ('For the mean type <1>, for the median type <2>, for the mode type <3>, to enter a new set of numbers type <4> or 5 to exit')
    import statistics
    if op == "1":
        mn = statistics.mean(the_list)
        print ("The mean of the values you have entered is:- ",mn)
    if op == "2":
        me = statistics.median(the_list)
        print ("The median of the values you have entered is:- ",me)
    if op == "3":
        mo = statistics.mode(the_list)
        print ("The mode of the values you have entered is:- ",mo)
    if op == "5":
        the_list = main()
    else:
        print("Goodbye")
        break

There are other options—you could pass in an empty list for main to fill, or use a global variable (or, better, a more restricted equivalent like an attribute on a class instance or a closure variable), or refactor your code so everyone who needs to access our_list is inside the same function… but I think this is the cleanest way to do what you're trying to do here.


By the way, this isn't quite the last hurdle—but you're very close:

  • After any mean, median, or mode, it's going to hit the "Goodbye" and exit instead of going back through the loop. Do you know about elif?
  • You mixed up '5' and '4' in the menu.
  • If the user enters 2 and 3 and asks for the mode, your code will dump a ValueError traceback to the screen; probably not what you want. Do you know try/except?

That's all I noticed, and they're all pretty simple things to add, so congrats in advance.

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

Comments

0

The issue is that our_list was defined in the main() function, and is not visible outside of the main() function scope.

Since you're doing everything in one chunk, you could remove line 1 and 6, taking the code from your main() function and putting it on the same indentation level as the code which follows.

Comments

0

This seems to be because you defined our_list within the main() function. You should probably define it as a global variable by creating it outside the main() function.

You could also put the while loop inside a function and pass in our_list as a parameter to the list.

1 Comment

global usually ends up with horrible code. It should be reserved for cases where it really is needed and the rest of the time you can just return values to the caller.

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.