Answered step by step
Verified Expert Solution
Link Copied!

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 problem-solving pattern is to call several functions in sequence, with the return value(s) of
each function call used as the argument(s) 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 3 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 1: 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 3 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 10,20
x , y = coords ()
Function 2: Converting to initials
Your next function should accept the first, middle, and last name as parameters, and return that persons
initials (i.e. 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 3: 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 3 digit number between 100 and 999(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 6

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Concepts of Database Management

Authors: Philip J. Pratt, Mary Z. Last

8th edition

1285427106, 978-1285427102

More Books

Students also viewed these Databases questions