In: Computer Science
Python 2.7 Question: If-Else Statement
Is there a way for me to ask the user a series of questions with an if-else statement? For example, if a statement is true ask these questions, if false ask these questions. This is what I have so far:
if user_input == "yes ":
ques.check_string("Enter Name: ")
ques.check_string("Enter email: ")
elif user_input != "yes":
ques.check_string("Enter Name: ")
ques.check_string("Enter phone number: ")
# The input() function allows user input after printing the string given as argument. You also have the option to give no argument. In that case, the program will not print anything before the user has to type his/her input.
# The lower() function converts all the letters in the given string to lower case. For example, if str = LeMoN, str.lower() = lemon.
# The code given below first takes a user input which is further used to identify which set of questions is to be asked by the if-else statement. The code also prints the inputs taken in the if-else statement for you to see and verify that the value you entered is stored. I would suggest you to change this code to your requirements and play around with it a little to assimilate how to take user input and handle it.
# Please refer to the screenshot of the code to understand the indentation.
# Take the first input by user without printing anything first,
and then store the input in the variable 'user_input'
user_input = input()
# Check if the variable user_input is 'yes' when all the letters
are lower case
if user_input.lower() == "yes":
name = input("Enter Name: ") # Prompt the user to enter name and
store the input in the variable 'name'
email = input("Enter email: ") # Prompt the user to enter e-mail
and store the input in the variable 'email'
print(name) # Prints name
print(email) # Prints email
# Check if the variable user_input is not 'yes' when all the
letters are lower case
elif user_input.lower() != "yes":
name = input("Enter Name: ") # Prompt the user to enter name and
store the input in the variable 'name'
phone = input("Enter phone number: ") # Prompt the user to enter
phone number and store the input in the variable 'phone'
print(name) # Prints name
print(phone) # Prints phone number