简体   繁体   English

如何在 if/else 结构中提问?

[英]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.我是 python 编程的新手,所以我希望我的问题有意义。

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.我从基本的 if/else 结构开始,但我被阻止了,因为我不知道如何在 if/else 结构中提问。 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.似乎我不能将input语句放在if语句中,因为当我运行时,它没有考虑B = input("Question 2?") But I need to assign a yes/no answer to Question 2 so I can pursue the interview.但我需要为问题 2 分配一个是/否的答案,这样我才能继续面试。 All I saw on the internet is print statements in if/else structure.我在互联网上看到的只是 if/else 结构中的print语句。 I also tried to do nested if statements but it doesn't work.我也尝试过嵌套if语句,但它不起作用。

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:你可以做一个elif语句,它是elseif组合,所以这意味着如果第一个条件不成立,检查这个是否成立:

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.我认为有人打败了我,但你应该将你的 if 语句链接在一起,以使事情变得更好。

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.使用您的字符串,您可以使用 the.lower() 使您的用户输入更加统一。 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')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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