#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    7
    Rep Power
    0

    Exclamation String type checker


    Hello everyone I'm new to python

    I'm stuck on this code
    I feel like I'm missing something small thats
    not bringing everything together:


    ######################################

    Write a function named safe_input (prompt, type)
    that works like the Python input function,
    except that it only accepts the specified type of input.

    The function takes two arguments:
    prompt: str
    type: int, float, str

    The function will keep prompting for input
    until correct input of the specified type is
    entered. The function returns the input.

    If the input was specified to be a number (float or int),
    the value returned will be of the correct type; that is,
    the function will perform the conversion.

    The default for a prompt is the empty string.
    The default for the type is string.

    ##########################################

    Heres what I have,

    PHP Code:
    safe_input input(str("Enter a String Type you want to check. \nPlease enter 'int', 'float', or 'str': "))

    def safe_input(prompttype_=str):
        if(
    type_ not in (strintfloat)): 
            
    raise ValueError("Expected str, int or float.")  

        while 
    True:
            
    test input(prompt)    
            try:
                
    ret type_(test)
            
    except ValueError:
                print(
    "Invalid type, enter again.")                
            else:
                break    

        return 
    ret


    test 
    safe_input("This is a string, how about another? ")
    print (
    '"{}" is a {}'.format(test,type(test)))
    test safe_input("This is a string, how about another?  ",int)
    print (
    '"{}" is a {}'.format(test,type(test)))
    test safe_input("This is a string , how about another? ",float)                      
    test safe_input(5)
    print (
    '"{}" is a {}'.format(test,type(test)))
    test safe_input(5,int)
    print (
    '"{}" is a {}'.format(test,type(test)))
    test safe_input(5,float)
    print (
    '"{}" is a {}'.format(test,type(test)))
    test safe_input(5.044)
    print (
    '"{}" is a {}'.format(test,type(test)))
    test safe_input(5.044int)
    print (
    '"{}" is a {}'.format(test,type(test)))
    test safe_input(5.044float)
    print (
    '"{}" is a {}'.format(test,type(test))) 
    It runs but it isn't returning the right answer consistently and no looping right or wrong answers.

    I thank you if anyone can help.


    I'm trying to do three things:

    what ever the user input enters, the right string type is given every time while looping to ask the user to enter again another.

    I am trying to loop the errors until the user enters the right format.

    Continue to loop user input to repeat.
    Last edited by tvj20xl; February 18th, 2013 at 01:31 AM. Reason: Clean up
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Location
    Iran
    Posts
    149
    Rep Power
    140
    I think you also need to check inside your function that the user specified type is a valid type and then of course check that the user input value corresponds to that specified type. Something similar to this:

    Code:
    def safe_input(param_prompt="", param_inputType=str):
        
        typesDictionary = {int:int, float:float, str:str}
       
        if param_inputType not in typesDictionary:
            raise ValueError("Unknown type specified")
        
        userInput = input(param_prompt)
        while type(userInput) != param_inputType:
            userInput = input(param_prompt)
        
        return param_inputType(userInput)
    
    
    
    ret = safe_input("enter value: ", int)
    print("The type of the returned value is: ", type(ret))

    Regards,
    Dariyoosh
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,995
    Rep Power
    481
    If you read from sys.stdin instead of using input you'll have a program that works in python 2 and 3.

    Other than not supplying a default for prompt I don't see a problem with your code if you run it in python3.

    In earlier versions of python input evaluates the string. You didn't mention the specific problem; I expect you saw NameError.

    Read from sys.stdin instead of using prompt---check to remove the trailing newline.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo