Help please, why doesn't it show the next input?

Oscar Benjamin oscar.j.benjamin at gmail.com
Thu Sep 12 05:39:33 EDT 2013


On 12 September 2013 07:04, William Bryant <gogobebe2 at gmail.com> wrote:
> Thanks everyone for helping but I did listen to you :3 Sorry. This is my code, it works, I know it's not the best way to do it and it's the long way round but it is one of my first programs ever and I'm happy with it:

Hi William, I'm glad you've solved your initial problem and I just
wanted to make a couple of comments about how your program could be
simplified or improved. The comments are below.

>
> '''#*************************************************************************'''
> #* Name:        Mode-Median-Mean Calculator                                   *#
> #*                                                                            *#
> #* Purpose:     To calculate the mode, median and mean of a list of numbers   *#
> #*              and the mode of a list of strings because that is what we are *#
> #*              learning in math atm in school :P                             *#
> #*                                                                            *#
> #* Author:      William Bryant                                                *#
> #*                                                                            *#
> #* Created:     11/09/2013                                                    *#
> #*                                                                            *#
> #* Copyright:   (c) William 2013                                              *#
> #*                                                                            *#
> #* Licence:     IDK :3                                                        *#
> '''**************************************************************************'''
>
>
>
>
> #-----#                   ~~Import things I am using~~                   #-----#
>
> #         |
> #        |
> #       \/
>
> import time
> import itertools
>
>
>
> #-----#        ~~Variables that I am using, including the list.~~        #-----#
>
> #         |
> #        |
> #       \/
>
> num_or_string = None
> amount_numbers = []
> List = []
> prompt = "Does your list contain, a number or a string?  \nEnter:  "
> user_inputHMNn = None
> user_inputHMNs = None

Of the declarations above only List and prompt are needed. Actually
though I wouldn't use either of those. List should really be a local
variable inside the functions below. And it would be better to write
prompt inline e.g.:

answer = input("Does your list contain, a number or a string?  \nEnter:  ")

That makes it easier to understand what's happening when you look at
the code. So I would remove all of those variable declarations. In
some other programming languages there are reasons to use declarations
like those above but not in Python.

>
> #-----#                   ~~Functions that I am using.~~                 #-----#
>
> #         |
> #        |
> #       \/
>
> user_inputNOS = input(prompt)
> user_inputNOS
> time.sleep(1.5)
>
> def NOS():
>     if user_inputNOS == "String" or user_inputNOS == "string" or user_inputNOS == "STRING" or user_inputNOS == "s" or user_inputNOS == "S" or user_inputNOS == "str":
>         HMNs()
>     elif user_inputNOS == "Number" or user_inputNOS == "number" or user_inputNOS == "NUMBER" or user_inputNOS == "N" or user_inputNOS == "N" or user_inputNOS == "int" or user_inputNOS == "num":
>         HMNn()
>     else:
>         global user_inputNOS2
>         global prompt
>         prompt = "You did not enter a valid field, :P Sorry.  \nEnter:  "
>         user_inputNOS2 = input(prompt)
>         user_inputNOS2
>         time.sleep(1.5)
>         NOS2()
>
> def NOS2():
>     if user_inputNOS2 == "String" or user_inputNOS2 == "string" or user_inputNOS2 == "STRING" or user_inputNOS2 == "s" or user_inputNOS2 == "S" or user_inputNOS2 == "str":
>         HMNs()
>     elif user_inputNOS2 == "Number" or user_inputNOS2 == "number" or user_inputNOS2 == "NUMBER" or user_inputNOS2 == "N" or user_inputNOS2 == "N" or user_inputNOS2 == "int" or user_inputNOS2 == "num":
>         HMNn()
>     else:
>         global prompt
>         prompt = "You did not enter a valid field, :P Sorry.  \nEnter:  "
>         user_inputNOS2
>         time.sleep(1.5)
>         NOS2()

The functions above are almost identical. The main difference is just
that NOS2() doesn't call input() which I assume is accidental. So you
could simplify this by only having one function NOS() and having it
call itself rather than NOS2() at the end. Then it would look like:

def NOS():
    # code here ...
    NOS()

However this is not a common way to use recursive function calls. What
this really does is repeat something forever in a loop. The common way
to do this is to use a for-loop or a while-loop. For example you can
get the same effect with:

while True:  # Loops forever (until the break)
    answer = input("Does your list contain, a number or a string?  \nEnter: ")
    answer = answer.lower()
    if answer in ("string", "str", "s"):
        HMNs()
    elif answer in ("number", "num", "n", "int"):
        HMNn()
    elif answer in ("quit", "q"):
        break  # Exits the while loop
    else:
        print("You did not enter a valid field, :P Sorry.")
     time.sleep(1.5)

You can put this code in a function or otherwise just move it below
the functions it calls (i.e. HMNs and HMNn) since code doesn't need to
be in functions in a Python script.

> def HMNs():
>     global TheStr, user_inputHMNs, List_input
>     user_inputHMNs = input("You picked string. This program cannot calculate the mean or median, but it can calculate the mode. :D  How many strings are you using in your list? (Can not be a decimal number)  \nEnter:  ")
>     user_inputHMNs
>     time.sleep(1.5)
>     TheStr = int(user_inputHMNs)
>     for i in range(TheStr):
>         List_input = input("Enter your strings. (One in each input field):  ")
>         List.append(List_input)
>         print("Your list -> ", List)
>
> def HMNn():
>     global TheNum, user_inputHMNn, List_input
>     user_inputHMNn = input("You picked number. :D How many numbers are you using in your list? (Can not be a decimal number) \nEnter:  ")
>     user_inputHMNn
>     time.sleep(1.5)
>     TheNum = int(user_inputHMNn)
>     for i in range(TheNum):
>         List_input = input("Enter your numbers. (One in each input field):  ")
>         List.append(List_input)
>         print("Your list -> ", List)

Using global variables in the functions above is confusing and
error-prone. The simpler and safer way to do it is to use local
variables in each function. To do this remove the global statements
and assign an empty list to the name List inside the function like
this:

def HMNn():
    List = []
    user_inputHMNn = ...
    # more code

Then you don't need to declare List at the top of your script.


Oscar



More information about the Python-list mailing list