#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    11
    Rep Power
    0

    Newbie needs help w/ If Else


    Hey Im writing this choose your own adventure thing to practice if else. I made it so they have to choose their character and I wanted to have it so they chose a b or c to get the corresponding character, but it only prints the message for the first character and I cant figure out why. Here is the code:

    Code:
    user_name = input("Welcome to the choose your own adventure program, please enter your name")
    
    print("Hello, " + user_name) 
    
    char = input("Choose your character: \n [a]Warrior \n [b]Mage \n [c]Healer")
    if char == 'a' or 'A':
        print ("You are a mighty Warrior!")
    elif char == 'b' or 'B':
        print ("You are a powerful Mage!")
    else:
        print ("You are a Healer, woohoo.")
        
    print("Our story begins in a magical kingdom, called The Whowhatsit.")
    It always gives the mighty warrior message. Im not seeing why.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    194
    Rep Power
    3
    This is a very common mistake for beginners:
    Code:
    if char == 'a' or 'A'
    does not work the way you expect.

    The two conditionals are checked seperately. First it checks
    Code:
    if char == 'a'
    then it checks:
    Code:
    if 'A'
    The second one is always true so that piece of code always runs.

    You want:
    Code:
    if char.lower() == 'a':
    or
    Code:
    if char in ('a','A'):
    -Mek
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    11
    Rep Power
    0
    Ok, now it is only printing the else statement of being a healer...am I supposed to put breaks in it or something?

    Here is the modified code:

    Code:
    user_name = input("Welcome to the choose your own adventure program, please enter your name")
    
    print("Hello, " + user_name) 
    
    char = input("Choose your character: \n [a]Warrior \n [b]Mage \n [c]Healer")
    if char.lower == 'a':
        print ("You are a mighty Warrior!")
    elif char.lower == 'b':
        print ("You are a powerful Mage!")
    elif char.lower == 'c':
        print ("You are a Healer, woohoo.")
    else:
        print ("Wrong Choice")
        
    print("Our story begins in a magical kingdom, called The Whowhatsit.")
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    194
    Rep Power
    3
    You forgot your parenthesis:

    You need:
    Code:
    if char.lower() == 'a':
    not:
    Code:
    if char.lower == 'a':
    -Mek
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    11
    Rep Power
    0
    Originally Posted by Mekire
    You forgot your parenthesis:

    You need:
    Code:
    if char.lower() == 'a':
    not:
    Code:
    if char.lower == 'a':
    -Mek
    Ahhhhh thank you very much
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2010
    Posts
    153
    Rep Power
    5
    Although you can code this way and it works, a more "Pythonic" way to do a big if/elif chain is to use a dict, like so:

    Code:
    char = input("Choose your character: \n [a]Warrior \n [b]Mage \n [c]Healer")
    character_messages = { 
        'a' : "You are a mighty Warrior!", 
        'b' : "You are a powerful Mage!", 
        'c' : "You are a Healer, woohoo."
        }
    print (character_messages.get(char.lower(), "Wrong Choice")
    print("Our story begins in a magical kingdom, called The Whowhatsit.")

IMN logo majestic logo threadwatch logo seochat tools logo