gpt4 book ai didi

python - 如何在 Python 3.2 中选择输入

转载 作者:行者123 更新时间:2023-11-28 22:01:15 25 4
gpt4 key购买 nike

input("Would you like to read: comedy, political, philisophical, or tragedy?")

a = "comedy"
b = "political"
c = "philisophical"
d = "tragedy"

if a:
input("Would you like the author's nationality to be: English or French?")
e = "French"
d = "English"
if e:
print("Tartuffe")
elif d:
print("Taming of the Shrew")

当我运行程序时,它只是默认为喜剧,然后是答尔丢夫。
如何让它识别字符串中的不同流派?

最佳答案

你需要存储输入,然后将它与你想要的进行比较,例如:

a = "comedy"
b = "political"
c = "philisophical"
d = "tragedy"

user_input = input("Would you like to read: comedy, political, philisophical, or tragedy?")

if user_input == a:
user_input = input("Would you like the author's nationality to be: English or French?")

if user_input == e:
#do more stuff

一个更好的方法(在我看来)是做这样的事情:

def comedy():
print("comedy")

def political():
print("political")

def philisophical():
print("philisophical")

def tragedy():
print("tragedy")

types = {"comedy":comedy,
"political":political,
"philisophical":philisophical,
"tragedy":tragedy
}

user_input = input()

types[user_input]()

因为它更容易管理和读取不同的输入。

关于python - 如何在 Python 3.2 中选择输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13262769/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com