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

私が実行すると、プログラムはデフォルトでコメディになり、次にTartuffeになります。
文字列のジャンルの違いを認識させるにはどうすればよいですか?

4

3 に答える 3

5

入力を保存してから、必要なものと比較する必要があります。次に例を示します。

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]()

さまざまな入力の管理と読み取りが簡単だからです。

于 2012-11-07T03:39:06.617 に答える
0

e の値が true かどうかをテストしているだけです (文字列は null ではないため、true です)。

入力も保存していません。

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

if selection == e:
    print("Tartuffe")
elif selection == d:
    print("Taming of the Shrew")
于 2012-11-07T03:41:56.720 に答える
0

拡張性の高いコード。

choices = {'e': ('French', 'Tartuffe'), 'd': ('English', 'Taming of the Shrew')}

cstr = ', '.join('%r = %s' % (k, choices[k][0]) for k in sorted(choices))
prompt = 'What would you like the author\'s nationality to be (%s): ' % cstr

i = input(prompt).lower()

print('%s: %s' % choices.get(i, ('Unknown', 'Untitled')))
于 2012-11-07T03:44:15.853 に答える