name

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • imran akhtar
    New Member
    • Nov 2008
    • 64

    name

    #Write a program which first asks the user for their full name.
    #It should then ask appropriate questions concerning their gender and
    #marital status in order to determine their title (Miss, Mrs or Mr).
    #Finally, it should greet the user formally using their title and
    #surname e.g. Hello Mr AKHTAR

    i have made this code as well, but want to know wht can be improved or chaged, to make it more effecient. there is an attached file as to see clearer

    Code:
    forename = raw_input ("Please enter your forename :")
    
    surname = raw_input ("Please enter your surname :")
    
    gender = raw_input ("Please enter in your gender information (male or female): ")
    
    maritalstatus = raw_input ("Please enter in your marital status information (single or married): ")
    
    if gender == "m" or gender == "M" or gender == "male" or gender == "Male":
        if maritalstatus == "single":   #user is male
            print "Hello Mr" + surname
        else:
            print "Hello Mr" + surname
    
    elif gender == "f" or gender == "F" or gender == "female" or gender == "Female":
        if maritalstatus == "single": #user is female
            print "Hello Miss" + surname
        else:
            print "Hello Mrs" + surname
    Attached Files
  • imran akhtar
    New Member
    • Nov 2008
    • 64

    #2
    can anyone help me out please

    Comment

    • Apostle
      New Member
      • Dec 2008
      • 28

      #3
      you have not said what you want us to do! Is it working, or need more improved version or what?

      Comment

      • boxfish
        Recognized Expert Contributor
        • Mar 2008
        • 469

        #4
        If you don't want your program to be case sensitive, try converting user input to all lowercase with the lower function:
        Code:
        gender = raw_input ("Please enter in your gender information (male or female): ").lower()
        Then you only need to test lowercase:
        Code:
        if gender == "m" or gender == "male"
        Hope this helps.

        Comment

        • imran akhtar
          New Member
          • Nov 2008
          • 64

          #5
          yes i want a more improved version of my code,

          Comment

          • bvdet
            Recognized Expert Specialist
            • Oct 2006
            • 2851

            #6
            Following is another way of testing the input. It also converts the input to lowercase.
            Code:
            if gender.lower() in ["m", "male"]:
                if maritalstatus.lower() == "single":

            Comment

            • imran akhtar
              New Member
              • Nov 2008
              • 64

              #7
              name code

              thnaks that works, but when i changed the code, for the female code, it does work. but when it gets to the lasts bit of code, when its asks for maritalstautus, it does not display, the final print message, it only works, if a female, says she is single.but if you chose married it does not work.

              Code:
              elif gender.lower() in ["f", "female"]:
                          if maritalstatus.lower()=="single":
                      print "Hello Miss" + surname
                  else:
                      print "Hello Mrs" + surname

              Comment

              • imran akhtar
                New Member
                • Nov 2008
                • 64

                #8
                can anyone please help

                Comment

                • bvdet
                  Recognized Expert Specialist
                  • Oct 2006
                  • 2851

                  #9
                  Indentation in Python is extremely important. You have an indentation error. Example of proper indentation:
                  Code:
                  >>> gender = 'FEMALE'
                  >>> surname = 'Smith'
                  >>> maritalstatus = 'married'
                  >>> if gender.lower() in ["m", "male"]:
                  ... 	pass
                  ... elif gender.lower() in ["f", "female"]:
                  ... 	if maritalstatus.lower()=="single":
                  ... 		print "Hello Miss " + surname
                  ... 	else:
                  ... 		print "Hello Mrs " + surname
                  ... 		
                  Hello Mrs Smith
                  >>>

                  Comment

                  • imran akhtar
                    New Member
                    • Nov 2008
                    • 64

                    #10
                    name code

                    no thing is it runs, and it works when you type in information, but when it comes to typin female, and when you type married, nothing out puts.

                    here is the codes how i have written it.
                    Code:
                    forename = raw_input("Enter your forename: ").swapcase()
                    
                    surname = raw_input ("Enter your surname :")
                    
                    gender = raw_input ("Enter in your gender information (male or female): ")
                    
                    maritalstatus = raw_input ("Enter in your marital status information (single or married): ")
                    
                    if gender.lower() in ["m", "male"]: 
                        if maritalstatus.lower() == "single":
                            print "Hello Mr" + surname
                        else:
                            print "Hello Mr" + surname
                    
                      
                    
                    elif gender.lower() in ["f", "female"]:
                        if maritalstatus.lower()=="single":
                                
                          print "Hello Miss" + surname
                    else:
                            print "Hello Mrs" + surname
                    but when you run it likes this. notice, when i types married.

                    Enter your forename: smith
                    Enter your surname :kelly
                    Enter in your gender information (male or female): female
                    Enter in your marital status information (single or married): married
                    >>> (here it spose to deipay the out put message . "hello mrs kelly" but it doe not.

                    Comment

                    • bvdet
                      Recognized Expert Specialist
                      • Oct 2006
                      • 2851

                      #11
                      There is an indentation error in your code.

                      -BV

                      Comment

                      Working...