0

I am trying to run a script which asks users for their favorite sports teams. This is what I have so far:

print("Who is your favorite sports team: Yankees, Knicks, or Jets?")
if input is "Yankees":
    print("Good choice, go Yankees")
elif input is "Knicks":
    print("Why...? They are terrible")
elif input is "Jets":
    print("They are terrible too...")
else:
    print("I have never heard of that team, try another team.")

Whenever I run this script, the last "else" function takes over before the user can input anything.

Also, none of the teams to choose from are defined. Help?

viraptor
  • 33,322
  • 10
  • 107
  • 191
holaprofesor
  • 271
  • 4
  • 15

2 Answers2

3

Input is a function that asks user for an answer.

You need to call it and assign the return value to some variable.

Then check that variable, not the input itself.

Note you probably want raw_input() instead to get the string you want.

Just remember to strip the whitespace.

Ethaan
  • 11,291
  • 5
  • 35
  • 45
viraptor
  • 33,322
  • 10
  • 107
  • 191
1

Your main problem is that you are using is to compare values. As it was discussed in the question here --> String comparison in Python: is vs. ==

You use == when comparing values and is when comparing identities.

You would want to change your code to look like this:

print("Who is your favorite sports team: Yankees, Knicks, or Jets?")
if input == "Yankees":
    print("Good choice, go Yankees")
elif input == "Knicks":
    print("Why...? They are terrible")
elif input == "Jets":
    print("They are terrible too...")
else:
    print("I have never heard of that team, try another team.")

However, you may want to consider putting your code into a while loop so that the user is asked the question until thy answer with an accepted answer.

You may also want to consider adding some human error tolerance, by forcing the compared value into lowercase letters. That way as long as the team name is spelled correctly, they comparison will be made accurately.

For example, see the code below:

while True: #This means that the loop will continue until a "break"
    answer = input("Who is your favorite sports team: Yankees, Knicks, or Jets? ").lower() 
#the .lower() is where the input is made lowercase
    if answer == "yankees":
        print("Good choice, go Yankees")
        break
    elif answer == "knicks":
        print("Why...? They are terrible")
        break
    elif answer == "jets":
        print("They are terrible too...")
        break
    else:
        print("I have never heard of that team, try another team.")
Community
  • 1
  • 1
Chironian
  • 45
  • 3