简体   繁体   中英

How do I do to ask something in if/else structure?

I am a very new beginner in python programming so I hope my question makes sense.

I want to make a program of a medical interview with guided questions depending on the user answers to previous questions.

I started with basic if/else structure but I am blocked because I don't know how to ask something in an if/else structure. For example:

A = input("Question 1 ? ")

if A == "yes":
    print("Consequence of yes to question 1")
else:
    A == "no"
    print("Consequence of no to question 1")

if A == "no":
    B = input("Question 2 ?")

It seems I can't put input statement in an if statement because when I run, it does not take the B = input("Question 2?") into account. But I need to assign a yes/no answer to Question 2 so I can pursue the interview. All I saw on the internet is print statements in if/else structure. I also tried to do nested if statements but it doesn't work.

Could someone help me please? Thanks a lot

You can do an elif statement, it's else and if combined, so what it means is that if the first condition isn't true, check if this one is true:

A = input("Question 1 ? ")

if A == "yes":
    print("Consequence of yes to question 1")
elif A == "no":        
    print("Consequence of no to question 1")

if A == "no":
    B = input("Question 2 ?")

I think someone beat me to it, but you should chain together your if statements to make things flow better.

a = input("Question 1 ? ")

if a == "yes":
    print("Consequence of yes to question 1")
elif a == "no":
    print("Consequence of no to question 1")
else:
    print('third statement')

b = input("Question 2 ?")

if b == "yes":
    print("Consequence of yes to question 2")
elif b == "no":
    print("Consequence of no to question 2")
else:
    print('third statement')

Since you're starting out and this is a pretty common track for people here's another tidbit for funzies. with your strings you can use the.lower() to make your user inputs a little more uniform. There is a lot you can do with strings to get the input standardized and hopefully this gets you rolling a bit.

a = input("Question 1 ? ")
##input into lowercase
a = a.lower()

if a == "yes":
    print("Consequence of yes to question 1")
elif a == "no":
    print("Consequence of no to question 1")
else:
    print('third statement')

b = input("Question 2 ?")
##input to lower case
b = b.lower()

if b == "yes":
    print("Consequence of yes to question 2")
elif b == "no":
    print("Consequence of no to question 2")
else:
    print('third statement')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM