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

Dave Angel davea at davea.name
Wed Sep 11 07:50:53 EDT 2013


On 11/9/2013 01:39, William Bryant wrote:

> On Wednesday, September 11, 2013 5:11:23 PM UTC+12, John Gordon wrote:
>> In <ef8de6db-5f35-4d07-8306-bcec47b1e69b at googlegroups.com> William Bryant <gogobebe2 at gmail.com> writes:
>> 
>> 
>> 
>> > Hey, I am very new to python, I am 13 years old. I want to be able to make =
>> 
>> > a program the caculates the mean, meadian and mode. When i run the program,=
>> 
>> >  an input field pops up and says 'Does your list contain, a number or a str=
>> 
>> > ing?' like I want it to, but when I type in something that is not one of va=
>> 
>> > lid field options: "String" or "string" or "STRING" or "s" or "S" or "str" =

    <etc.>

Thanks for quoting some context.  However, since you're using buggy
googlegroups, please either constrain your quoting to a couple of lines,
or fix its display to NOT doublespace everything.

See  http://wiki.python.org/moin/GoogleGroupsPython


>
> Thanks so much for putting your time into helping me!! It worked! Could you tell me if I did anything wrong this time? :)
>
> #-----           Variables that I am using, including the list.          -----#
>
> num_or_string = ""
> amount_numbers = []
> List = []
> prompt = "Does your list contain, a number or a string?"
>
> #-----                      Functions that I am using.                    -----#
> user_input1 = input(prompt)
> user_input1

This last line does nothing useful.  You may be confused because in the
debugger, a lone expression getes its value printed.  But that's a
debugger feature not a language one.  If you want to see what got
input, you'd want to use print() function.


Looks like the second function is identical to the first.  if I'm right,
then just change the NOS2 into NOS1, and save lots of redundancy.

>
> def NOS():
>     if user_input1 == "String" or user_input1 == "string" or user_input1 == "STRING" or user_input1 == "s" or user_input1 == "S" or user_input1 == "str":
>         print("a")
>     elif user_input1 == "Number" or user_input1 == "number" or user_input1 == "NUMBER" or user_input1 == "N" or user_input1 == "N" or user_input1 == "int" or user_input1 == "num":
>         print("a")
>     else:
>         global user_input2
>         global prompt
>         prompt = "You did not enter a valid field, :P Sorry."
>         user_input2 = input(prompt)
>         user_input2
>         NOS2()

Here you're using the recursive call to NOS() to substitute for a loop. 
Recursion is very useful, but in this case, it hides what's really
happening, and it prevents you from writing a conventional function with
parameters and return value.

I don't know what NOS is supposed to stand for, but it's generally
useful if a function does a simply described single thing.  In this
case, it gets input from the user, and asks the user repeatedly till
he/she gets it right.  So the function should do its own input, and not
expect the top-level code to already have asked once.

Something like:

SOMETHING = [ ....   list of the valid inputs  ]

def query_user(prompt):
    while true:
        udata = input(prompt)
        if        udata.lower() in SOMETHING
            return udata
        print("Invalid field, sorry")

And now you call this function the same way you would have called
input() function:

user_input1 = query_user("Does your list contain, a number or a
string?")


>
> NOS()

-- 
DaveA




More information about the Python-list mailing list