Question

In: Computer Science

Python 2.7 Question: If-Else Statement Is there a way for me to ask the user a...

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: ")
  

Solutions

Expert Solution

# 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


Related Solutions

Flowchart + Python. Ask the user for a value. Then, ask the user for the number...
Flowchart + Python. Ask the user for a value. Then, ask the user for the number of expressions of that value. Use While Loops to make a program. So then, it should be so that 5 expressions for the value 9 would be: 9 * 1 = 9 9 * 2 = 18 9 * 3 = 27 9 * 4 = 36 9 * 5 = 45 Flowcharts and Python Code. Not just Python code. I cannot read handwriting....
Program a math quiz on Python Specifications: - The program should ask the user a question...
Program a math quiz on Python Specifications: - The program should ask the user a question and allow the student to enter the answer. If the answer is correct, a message of congratulations should be displayed. If the answer is incorrect, a message showing the correct answer should be displayed. - the program should use three functions, one function to generate addition questions, one for subtraction questions, and one for multiplication questions. - the program should store the questions and...
Python #Use an input statement to ask the user for his/her age (no prompt) and assign...
Python #Use an input statement to ask the user for his/her age (no prompt) and assign the user entry to a variable #Convert the user input to an integer and assign to a variable #Write if-elif-else statements with the following conditions and actions #Important: For each Action below, use the same variable name for the rating in all the if, elif, and else statements #Condition: user input less than or equal to 9 Action: assign the letter G as a...
In python. Projectile motion: Write a python program that will ask the user for      an...
In python. Projectile motion: Write a python program that will ask the user for      an initial height y0, initial velocity v, launch angle theta, and mass m.      Create two functions, one that will calculate max height      of the projectile, and one that will calculate the range. Ask the     user which one he/she would like to calculate, then present them with the answer. (use kg, m and m/s)
PYTHON Ask the user to enter the number of students in the class. Thereafter ask the...
PYTHON Ask the user to enter the number of students in the class. Thereafter ask the user to enter the student names and scores as shown. Output the name and score of the student with the 2nd lowest score. NOTE: You may assume all students have distinct scores between 0 and 100 inclusive and there are at least 2 students NOTE: You may only use what has been taught in class so far for this assignment. You are not allowed...
Create a program (Python) YourFirstnameLastnameA06b.py to ask the user to create a password: The user will...
Create a program (Python) YourFirstnameLastnameA06b.py to ask the user to create a password: The user will first enter a password, then enters the same password again; If the second input is the same as first one, the user successfully creates the password. Print “Well done.”; Otherwise, the user will be directed to repeat the whole process (go to step 1.)
Calculating Delivery Cost Program in Python write a program in Python that will ask a user...
Calculating Delivery Cost Program in Python write a program in Python that will ask a user to enter the purchase total, the number of the items that need to be delivered and delivery day. Then the system displays the cost of delivery along with the total cost. Purchase total > $150 Yes Number of the items (N) N<=5 N>=6 Delivery day Same Day Next Day Same Day Next Day Delivery charges ($) 8 N * 1.50 N * 2.50 N...
In python Write the code to ask a user to enter a number in the range...
In python Write the code to ask a user to enter a number in the range of 1-100. Have the code checked to make sure that it is in this range. If it is not, let the user re-enter the number. The user should be able to re-enter numbers until the number is within the correct range.
Write a Python program that will ask the user to input a word, will return the...
Write a Python program that will ask the user to input a word, will return the first letter of the input word, and ask the user to put another word, and so on, in the form of a loop. If the user chooses to stop, he or she should input the integer "0" for the loop to stop.
PYTHON Modify the program in section Ask the user for a first name and a last...
PYTHON Modify the program in section Ask the user for a first name and a last name of several people.  Use a loop to ask for user input of each person’s first and last names  Each time through the loop, use a dictionary to store the first and last names of that person  Add that dictionary to a list to create a master list of the names  Example dictionary: aDict = { "fname":"Douglas", "name":"Lee" } ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT