********PYTHON******** Write a function called name_facts that will take a first
ID: 3754255 • Letter: #
Question
********PYTHON********
Write a function called name_facts that will take a firstname (string) as an input parameter, and print out various facts about the name, including: 1) its length, 2) whether it ends with the letter R, 3) how many of its letters are vowels (NOTE: not the number of unique vowels, but the total number of vowels), and 4) whether it contains a Q. To gain full credit for this exercise, you must use string formatting to print out the result.¶
Hints:
You will probably want to convert the string to lowercase when checking conditions 2-4. You can get by without it, but you'll have to make sure you check both lower and uppercase versions of the letters
You will have to use the in operator for condition 3 and 4
You will probably want to use an algorithm like this for condition 3:
You will also probably want to make a separate message for each condition (depending on the answer) and use string formatting to join them into the final message
Example output:
Given "Allegra" as input: Your name is 7 letters long, does not end with the letter S, contains 3 vowels, and does not contain a Q or Y
Given "Xander" as input: Your name is 6 letters long, does end with the letter R, contains 2 vowels, and does not contain a Q or Y
Given "Queen" as input: Your name is 5 letters long, does not end with the letter R, contains 3 vowels, and does contain a Q or Y
Explanation / Answer
[Note]: I have added print statement for 4) as contain a Q or Y as it has been mentioned in output sample
#########Code below#######
def name_facts(firstname):
length = len(firstname)
print "Your name is",length,"letters long,", # 1) print its length
if firstname.lower().endswith('r'):
print "does end with letter R,", # 2) print whether it ends with the letter R
else:
print "does not end with letter R,", # 2) print whether it ends with the letter R
num_vowels=0
for character in firstname:
if set('aeiou').intersection(character.lower()):
num_vowels=num_vowels+1;
print "contains",num_vowels,"vowels,", # 3) print total number of vowels
q_present=0
for character in firstname:
if character.lower() == 'q':
q_present=q_present+1;
if q_present==0:
print "and does not contain a Q or Y" # 4) print whether it contains a Q
else:
print "and does contain a Q or Y" # 4) print whether it contains a Q
if __name__== "__main__":
name_facts("Allegra")
name_facts("Xander")
name_facts("Queen")
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.