Skip to main content
edited body
Source Link

Good question! You can try the following code for this. =)

This code uses ast.literal_eval() to find the data type of the input (age). Then it follows the following algorithm:

  1. Ask user to input her/his age.

1.1. If age is float or int data type:

 - Check if `age>18``age>=18`. If `age>18``age>=18`, print appropriate output and exit. 
 - Check if `0<age<=18``0<age<18`. If `0<age<=18``0<age<18`, print appropriate output and exit. 
 - If `age<=0`, ask the user to input a valid number for age again, (*i.e.* go back to step 1.) 

1.2. If age is not float or int data type, then ask user to input her/his age again (i.e. go back to step 1.)

Here is the code.

from ast import literal_eval ''' This function is used to identify the data type of input data.''' def input_type(input_data): try: return type(literal_eval(input_data)) except (ValueError, SyntaxError): return str flag = True while(flag): age = raw_input("Please enter your age: ") if input_type(age)==float or input_type(age)==int: if eval(age)>18>=18: print("You are able to vote in the United States!") flag = False elif eval(age)>0 and eval(age)<=18<18: print("You are not able to vote in the United States.") flag = False else: print("Please enter a valid number as your age.") else: print("Sorry, I didn't understand that.") 

Good question! You can try the following code for this. =)

This code uses ast.literal_eval() to find the data type of the input (age). Then it follows the following algorithm:

  1. Ask user to input her/his age.

1.1. If age is float or int data type:

 - Check if `age>18`. If `age>18`, print appropriate output and exit. 
 - Check if `0<age<=18`. If `0<age<=18`, print appropriate output and exit. 
 - If `age<=0`, ask the user to input a valid number for age again, (*i.e.* go back to step 1.) 

1.2. If age is not float or int data type, then ask user to input her/his age again (i.e. go back to step 1.)

Here is the code.

from ast import literal_eval ''' This function is used to identify the data type of input data.''' def input_type(input_data): try: return type(literal_eval(input_data)) except (ValueError, SyntaxError): return str flag = True while(flag): age = raw_input("Please enter your age: ") if input_type(age)==float or input_type(age)==int: if eval(age)>18: print("You are able to vote in the United States!") flag = False elif eval(age)>0 and eval(age)<=18: print("You are not able to vote in the United States.") flag = False else: print("Please enter a valid number as your age.") else: print("Sorry, I didn't understand that.") 

Good question! You can try the following code for this. =)

This code uses ast.literal_eval() to find the data type of the input (age). Then it follows the following algorithm:

  1. Ask user to input her/his age.

1.1. If age is float or int data type:

 - Check if `age>=18`. If `age>=18`, print appropriate output and exit. 
 - Check if `0<age<18`. If `0<age<18`, print appropriate output and exit. 
 - If `age<=0`, ask the user to input a valid number for age again, (*i.e.* go back to step 1.) 

1.2. If age is not float or int data type, then ask user to input her/his age again (i.e. go back to step 1.)

Here is the code.

from ast import literal_eval ''' This function is used to identify the data type of input data.''' def input_type(input_data): try: return type(literal_eval(input_data)) except (ValueError, SyntaxError): return str flag = True while(flag): age = raw_input("Please enter your age: ") if input_type(age)==float or input_type(age)==int: if eval(age)>=18: print("You are able to vote in the United States!") flag = False elif eval(age)>0 and eval(age)<18: print("You are not able to vote in the United States.") flag = False else: print("Please enter a valid number as your age.") else: print("Sorry, I didn't understand that.") 
added 585 characters in body
Source Link
  • If age is float or int data type:

    - Check if `age>18`. If `age>18`, print appropriate output and exit. - Check if `0<age<=18`. If `0<age<=18`, print appropriate output and exit. - If `age<=0`, ask the user to input a valid number for age again. 
  • If age is not float or int data type, then ask user to input her/his age again.

  1. Ask user to input her/his age.

1.1. If age is float or int data type:

 - Check if `age>18`. If `age>18`, print appropriate output and exit. 
 - Check if `0<age<=18`. If `0<age<=18`, print appropriate output and exit. 
 - If `age<=0`, ask the user to input a valid number for age again, (*i.e.* go back to step 1.) 

1.2. If age is not float or int data type, then ask user to input her/his age again (i.e. go back to step 1.)

  • If age is float or int data type:

    - Check if `age>18`. If `age>18`, print appropriate output and exit. - Check if `0<age<=18`. If `0<age<=18`, print appropriate output and exit. - If `age<=0`, ask the user to input a valid number for age again. 
  • If age is not float or int data type, then ask user to input her/his age again.

  1. Ask user to input her/his age.

1.1. If age is float or int data type:

 - Check if `age>18`. If `age>18`, print appropriate output and exit. 
 - Check if `0<age<=18`. If `0<age<=18`, print appropriate output and exit. 
 - If `age<=0`, ask the user to input a valid number for age again, (*i.e.* go back to step 1.) 

1.2. If age is not float or int data type, then ask user to input her/his age again (i.e. go back to step 1.)

added 585 characters in body
Source Link

Good question! You can try the following code for this. =)

This code uses ast.literal_eval() to find the data type of the input (age). Then it follows the following algorithm:

  • If age is float or int data type:

    - Check if `age>18`. If `age>18`, print appropriate output and exit. - Check if `0<age<=18`. If `0<age<=18`, print appropriate output and exit. - If `age<=0`, ask the user to input a valid number for age again. 
  • If age is not float or int data type, then ask user to input her/his age again.

Here is the code.

from ast import literal_eval ''' This function is used to identify the data type of input data.''' def input_type(input_data): try: return type(literal_eval(input_data)) except (ValueError, SyntaxError): return str flag = True while(flag): age = raw_input("Please enter your age: ") if input_type(age)==float or input_type(age)==int: if eval(age)>18: print("You are able to vote in the United States!") flag = False elif eval(age)>0 and eval(age)<=18: print("You are not able to vote in the United States.") flag = False else: print("Please enter a correctvalid number as your age.") else: print("Sorry, I didn't understand that.") 

Good question! You can try the following code for this. =)

from ast import literal_eval ''' This function is used to identify the data type of input data.''' def input_type(input_data): try: return type(literal_eval(input_data)) except (ValueError, SyntaxError): return str flag = True while(flag): age = raw_input("Please enter your age: ") if input_type(age)==float or input_type(age)==int: if eval(age)>18: print("You are able to vote in the United States!") flag = False elif eval(age)>0 and eval(age)<=18: print("You are not able to vote in the United States.") flag = False else: print("Please enter a correct number as your age.") else: print("Sorry, I didn't understand that.") 

Good question! You can try the following code for this. =)

This code uses ast.literal_eval() to find the data type of the input (age). Then it follows the following algorithm:

  • If age is float or int data type:

    - Check if `age>18`. If `age>18`, print appropriate output and exit. - Check if `0<age<=18`. If `0<age<=18`, print appropriate output and exit. - If `age<=0`, ask the user to input a valid number for age again. 
  • If age is not float or int data type, then ask user to input her/his age again.

Here is the code.

from ast import literal_eval ''' This function is used to identify the data type of input data.''' def input_type(input_data): try: return type(literal_eval(input_data)) except (ValueError, SyntaxError): return str flag = True while(flag): age = raw_input("Please enter your age: ") if input_type(age)==float or input_type(age)==int: if eval(age)>18: print("You are able to vote in the United States!") flag = False elif eval(age)>0 and eval(age)<=18: print("You are not able to vote in the United States.") flag = False else: print("Please enter a valid number as your age.") else: print("Sorry, I didn't understand that.") 
Source Link
Loading
Post Made Community Wiki by Siddharth Satpathy