#Recall that input from a user is always in the form of a string. #Write a function called "input_type" that gets user input and #determines what kind of string the user entered. The user input #will be supplied as an argument to the function like normal. # # - Your function should return "integer" if the string only # contains characters 0-9. # - Your function should return "float" if the string only # contains the numbers 0-9 and at most one period. # - You should return "boolean" if the user enters "True" or # "False". # - Otherwise, you should return "string". #Write your function here!

Respuesta :

Answer:

Here is the Python program:

def input_type(input_string): #function that takes a string as input and returns the type of that string

   try:   #used to test a block of code for input string type              

       if type(eval(input_string)) == float: #if the type of the string is float

           return "float" #function returns float

       elif type(eval(input_string)) == int: #else if the type of the string is int

           return "integer"#function return integers

       elif type(eval(input_string)) == bool:#else if the type of the string is bool

           return "boolean" #function return boolean

   except: #if the type of the string is neither int, bool nor float

       return "string" #function return boolean

#following statements are used to  test the working of the above function

print(input_type(" ")) #prints string

print (input_type ("False")) #prints boolean

print (input_type ("7.432621")) #prints  float

print (input_type("2788"))#prints int

Explanation:

The function input_type() takes a user input and returns the type of that string input the user entered. The user input can be one of the following types:

int which is the data type of an integer for example 1, 2, 3 etc

float which is the data type of a floating point number for example 1.2, 3.4 etc

bool which is the data type of a boolean for example True or False

string that can be characters for example an empty space character " "

try block here is used to generate an exception and it is used to test a block in the code that checks for the type of the user input. The except block here is used to handle that error. This is executed when the try part raises an error.

Inside the try block, the if and elif statements check the type of the string.

if type(eval(input_string)) == float: statement checks if the type of the string is float. Here the eval() method is used to convert the input string because the user input is basically a string value. So this method evaluates the input_string.

type() method is used to return the type of that evaluated user input string. If condition checks if type(eval(input_string)) which is the type of the evaluated input string is equal to float. If this condition evaluates to true then the function returns "float" otherwise it moves to the next elif statement. Similarly the type of the user input is checked for int in elif part and if both these if and elif conditions evaluate to false then the next elif statement is executed that checks the user input to be bool. If all these three if elif statements evaluate to false then the except part executes which returns "string". The program along with its output is attached in a screenshot.    

Ver imagen mahamnasir