user1769560
user1769560

Reputation: 33

How to choose between input in Python 3.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")

When I run the program is just defaults to comedy and then to Tartuffe.
How do I get it to recognize the difference genres in the string?

Upvotes: 2

Views: 1736

Answers (3)

Serdalis
Serdalis

Reputation: 10489

you need to store the input and then compare it to what you want, for example:

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

A better way to do this (in my opinion) would be to do something like:

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

because its easier to manage and read the different inputs.

Upvotes: 5

pydsigner
pydsigner

Reputation: 2885

Highly extensible code.

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')))

Upvotes: 0

MoRe
MoRe

Reputation: 1538

You are just testing if the value of e is true (the string is not null, hence it is true).

You are not storing the input either.

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

Upvotes: 0

Related Questions