By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,907 Members | 2,031 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,907 IT Pros & Developers. It's quick & easy.

name

P: 64
#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

Expand|Select|Wrap|Line Numbers
  1. forename = raw_input ("Please enter your forename :")
  2.  
  3. surname = raw_input ("Please enter your surname :")
  4.  
  5. gender = raw_input ("Please enter in your gender information (male or female): ")
  6.  
  7. maritalstatus = raw_input ("Please enter in your marital status information (single or married): ")
  8.  
  9. if gender == "m" or gender == "M" or gender == "male" or gender == "Male":
  10.     if maritalstatus == "single":   #user is male
  11.         print "Hello Mr" + surname
  12.     else:
  13.         print "Hello Mr" + surname
  14.  
  15. elif gender == "f" or gender == "F" or gender == "female" or gender == "Female":
  16.     if maritalstatus == "single": #user is female
  17.         print "Hello Miss" + surname
  18.     else:
  19.         print "Hello Mrs" + surname
Attached Files
File Type: txt name.txt (735 Bytes, 320 views)
Dec 17 '08 #1
Share this Question
Share on Google+
10 Replies


P: 64
can anyone help me out please
Dec 18 '08 #2

P: 28
you have not said what you want us to do! Is it working, or need more improved version or what?
Dec 18 '08 #3

boxfish
Expert 100+
P: 469
If you don't want your program to be case sensitive, try converting user input to all lowercase with the lower function:
Expand|Select|Wrap|Line Numbers
  1. gender = raw_input ("Please enter in your gender information (male or female): ").lower()
Then you only need to test lowercase:
Expand|Select|Wrap|Line Numbers
  1. if gender == "m" or gender == "male"
Hope this helps.
Dec 18 '08 #4

P: 64
yes i want a more improved version of my code,
Dec 19 '08 #5

bvdet
Expert Mod 2.5K+
P: 2,851
Following is another way of testing the input. It also converts the input to lowercase.
Expand|Select|Wrap|Line Numbers
  1. if gender.lower() in ["m", "male"]:
  2.     if maritalstatus.lower() == "single":
Dec 19 '08 #6

P: 64
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.

Expand|Select|Wrap|Line Numbers
  1. elif gender.lower() in ["f", "female"]:
  2.             if maritalstatus.lower()=="single":
  3.         print "Hello Miss" + surname
  4.     else:
  5.         print "Hello Mrs" + surname
Dec 20 '08 #7

P: 64
can anyone please help
Dec 20 '08 #8

bvdet
Expert Mod 2.5K+
P: 2,851
Indentation in Python is extremely important. You have an indentation error. Example of proper indentation:
Expand|Select|Wrap|Line Numbers
  1. >>> gender = 'FEMALE'
  2. >>> surname = 'Smith'
  3. >>> maritalstatus = 'married'
  4. >>> if gender.lower() in ["m", "male"]:
  5. ...     pass
  6. ... elif gender.lower() in ["f", "female"]:
  7. ...     if maritalstatus.lower()=="single":
  8. ...         print "Hello Miss " + surname
  9. ...     else:
  10. ...         print "Hello Mrs " + surname
  11. ...         
  12. Hello Mrs Smith
  13. >>> 
Dec 20 '08 #9

P: 64
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.
Expand|Select|Wrap|Line Numbers
  1. forename = raw_input("Enter your forename: ").swapcase()
  2.  
  3. surname = raw_input ("Enter your surname :")
  4.  
  5. gender = raw_input ("Enter in your gender information (male or female): ")
  6.  
  7. maritalstatus = raw_input ("Enter in your marital status information (single or married): ")
  8.  
  9. if gender.lower() in ["m", "male"]: 
  10.     if maritalstatus.lower() == "single":
  11.         print "Hello Mr" + surname
  12.     else:
  13.         print "Hello Mr" + surname
  14.  
  15.  
  16.  
  17. elif gender.lower() in ["f", "female"]:
  18.     if maritalstatus.lower()=="single":
  19.  
  20.       print "Hello Miss" + surname
  21. else:
  22.         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.
Dec 20 '08 #10

bvdet
Expert Mod 2.5K+
P: 2,851
There is an indentation error in your code.

-BV
Dec 20 '08 #11

Post your reply

Sign in to post your reply or Sign up for a free account.