Answered step by step
Verified Expert Solution
Question
1 Approved Answer
To write functions meant to be used in sequence A common problem - solving pattern is to call several functions in sequence, with the return
To write functions meant to be used in sequence
A common problemsolving pattern is to call several functions in sequence, with the return values of
each function call used as the arguments for the NEXT function call. Youll practice this technique here.
Generating a Username
Our overall goal is to generate a partially randomized username based on a users personal initials. But
well divide the work up into separate functions, which the main part of your script will then call in order.
Normally, when you are breaking up a problem into parts, its up to you to decide how many functions to
have and what their inputs and outputs should be However, since this is for practice, in this case just be
sure to read the instructions carefully and follow them exactly.
Function : Getting the users name
Write a function that uses the input function to obtain the users first, middle, and last name via console
input. The user can leave some names blank which means the matching name will get set to the empty
string Note that its ok to put console input inside a function like this, so long as its the MAIN purpose of
the function!
The program should format each name such that no matter what the user types the first letter of each
name is capitalized and the rest are all lower case hint: use the string method title
Finally, the function should return all names. In Python, having multiple return values is easy! Just sepa
rate the values you want to return with commas after the return statement, and when calling the function,
put multiple variables on the left hand of the equal sign to "collect" each return value. Here is an example:
def coords :
return
x y coords
Function : Converting to initials
Your next function should accept the first, middle, and last name as parameters, and return that persons
initials ie the first letter of each of their names as a single string.
Recall that one or more of the names may have been left blank. If this happens, then use a capital letter
X for that name in the persons initials. For example, "Ash Ketchum" should have initials of "AXK", and
"Pikachu" should have initials of PXX
Function : Generating the username
Your final function should accept a persons initials as a parameter and return a username consisting of the
initials converted to lower case and followed by a random digit number between and inclusive
Hint: import the random module and use the randint method.
Main program
The "main" part of your program should call each function in sequence, passing along their return values
as needed, and then display a message to the user indicating their generated username.
Page
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started