繁体   English   中英

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

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

我是 python 编程的新手,所以我希望我的问题有意义。

我想根据用户对先前问题的回答制作一个带有引导性问题的医学访谈程序。

我从基本的 if/else 结构开始,但我被阻止了,因为我不知道如何在 if/else 结构中提问。 例如:

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

似乎我不能将input语句放在if语句中,因为当我运行时,它没有考虑B = input("Question 2?") 但我需要为问题 2 分配一个是/否的答案,这样我才能继续面试。 我在互联网上看到的只是 if/else 结构中的print语句。 我也尝试过嵌套if语句,但它不起作用。

有人可以帮我吗? 非常感谢

你可以做一个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 ?")

我认为有人打败了我,但你应该将你的 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')

因为你刚开始,这对人们来说是一个很常见的轨道,这里是另一个有趣的花絮。 使用您的字符串,您可以使用 the.lower() 使您的用户输入更加统一。 你可以用字符串做很多事情来使输入标准化,希望这能让你有所收获。

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