dimanche 30 septembre 2018

What is wrong with my if-else statements and variable assignments?

Goal: I'm trying to allow the user to input varying spellings for certain auto services, and still have my code work properly. My goal was to use the if-else statements to ensure that if the user misspelled a service, the code could correct the error by changing the variable assignment to the string that matched my dictionary key.

The issue: The code will output: Tire rotation for any input that I enter for auto_serv. Where have I made a mistake? Any better ideas to program this? Keep in mind, I'm a first time programmer that's doing this for class, and I just learned if-else statements.

The code:

# dictionary assigns the cost for each service
services = {'Oil change': 35,
            'Tire rotation': 19,
            'Car wash': 7,
            'Car wax': 12}
# auto_serv is the user's desired car service
auto_serv = input('Desired auto service:')
# The following four 'if' statements are to allow the user multiple variances in spelling of desired auto service
if auto_serv == 'Tirertation' or 'tirerotation':

    auto_serv = 'Tire rotation'
elif auto_serv == 'Oilchange' or 'oilchange':

    auto_serv = 'Oil change'
elif auto_serv == 'Carwash' or 'carwash':

    auto_serv = 'Car wash'
elif auto_serv == 'Carwax' or 'carwax':

    auto_serv = 'Car wax'

# the if-else statements are to give a result for each service the user requests
if auto_serv == 'Tire rotation':

    print('You entered:', auto_serv)

    print('Cost of %s: $%d' % (auto_serv, services[auto_serv]))
elif auto_serv == 'Oil change':

    print('You entered:', auto_serv)

    print('Cost of %s: $%d' % (auto_serv, services[auto_serv]))
# ...there are more elif statements that follow this code with the other auto services

Aucun commentaire:

Enregistrer un commentaire