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

    Join Date
    Oct 2013
    Posts
    3
    Rep Power
    0

    Help with code else python 3.3.2


    Hi, I made a very basic program and I cant figure out why its doing what its doing. i have it so when you input your name the program says thats a nice name but I also have it set up so that when you use a specific name it will say something other then thats a nice name. For some reason it says a different message plus thats a nice name even though I dont want it to. I want it to just say the specific message its supposed to.
    It only works the way its supposed to when you use the name cher. Im really lost. Maybe im not using the else input properly?
    Help would be greatly appreciated.

    print ("welcome")
    print ()
    password = input ("Please enter your password: ")

    while password != "hello" :
    password = input ("Please enter your password: ")
    print ("Access granted, welcome to the second part of the program")


    name = input ("Enter your name: ")


    if name == ("Benoit"):
    print ("What a great name!")
    if name == ("benoit"):
    print ("What a great name!")
    if name == "Madonna":
    print ("May I have your autograph, please?")
    if name == "madonna":
    print ("May I have your autograph, please?")
    if name == "Cher":
    print ("May I have your autograph please?")
    if name == "cher":
    print ("May I have your autograph, please?")

    else :
    print ("That's a nice name")
    print ()


    input ("press enter to exit")


    Code:
    print ("welcome") print () password = input ("Please enter your password: ")  while password != "hello" :      password = input ("Please enter your password: ") print ("Access granted, welcome to the second part of the program")   name = input ("Enter your name: ")   if name == ("Benoit"):     print ("What a great name!") if name == ("benoit"):      print ("What a great name!") if name == "Madonna":     print ("May I have your autograph, please?") if name == "madonna":      print ("May I have your autograph, please?") if name == "Cher":     print ("May I have your autograph please?") if name == "cher":      print ("May I have your autograph, please?")  else :      print ("That's a nice name") print ()   input ("press enter to exit")
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Oct 2009
    Location
    Nebraska, USA
    Posts
    876
    Rep Power
    275
    I believe this is what you are looking for:
    Code:
    print ("welcome")
    print ()
    password = input ("Please enter your password: ")
    
    while password != "hello" :
        password = input ("Please enter your password: ")
    print ("Access granted, welcome to the second part of the program")
    
    name = input ("Enter your name: ")
    
    if name.lower() == "benoit":
        print ("What a great name!")
    elif name.lower() == "madonna":
        print ("May I have your autograph, please?")
    elif name.lower() == "cher":
        print ("May I have your autograph please?")
    else :
        print ("That's a nice name")
        
    print ()
    
    
    input ("press enter to exit")
    Last edited by DonR; October 29th, 2013 at 01:24 AM.
  4. #3
  5. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    611
    Rep Power
    65
    Mildly simpler ...
    Code:
    print("welcome\n")
    
    while True:
        password = input("Please enter your password: ")
        if password == "hello":
            break
    
    print("Access granted, welcome to the second part of the program")
    
    name = input("Enter your name: ")
    
    if name.lower() == "benoit":
        print ("What a great name!")
    elif name.lower() in ("madonna", "cher"):
        print ("May I have your autograph, please?")
    else:
        print ("That's a nice name")
        
    print()
    
    input("press enter to exit")
    Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,899
    Rep Power
    481

    OK, since you're all posting...I complain


    NOT OK! BAD! CONTAINS DUPLICATE CODE. WE ABHOR DUPLICATE CODE.

    name.lower()
    name.lower()

    name.long_running()
    name.long_running()
    name.long_running()

    name.i_wanna_change_this()
    name.i_wanna_change_this()

    name.i_need_ta_change_this()
    name.i_need_ta_change_this()
    name.i_need_ta_change_this()

    Better:
    Code:
    name_lower_case = name.lower()
    if name_lower_case == "benoit":
        print ("What a great name!")
    elif name_lower_case  == "madonna":
        print ("May I have your autograph, please?")
    elif name_lower_case  == "cher":
        print ("May I have your autograph please?")
    else :
        print ("That's a nice name")
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    611
    Rep Power
    65
    Simpler ..
    Code:
    print("Welcome\n")
    
    while True:
        password = input("Please enter your password: ")
        if password == "hello":
            break
    
    print("Access granted, welcome to the second part of the program")
    
    name = input("Enter your name: ").lower()
    
    if name == "benoit":
        print ("What a great name!")
    elif name in ("madonna", "cher"):
        print ("May I have your autograph, please?")
    else:
        print ("That's a nice name")
        
    input("\nPress enter to exit")
    Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25

IMN logo majestic logo threadwatch logo seochat tools logo