Question: Write a Python script that does the following: 1. Call a function to print a message to the screen directing the user to type in

Write a Python script that does the following:

1. Call a function to print a message to the screen directing the user to type in their first name

2. Read in the first name and store it in its own variable

Notice that the previous step could be done with this one in one statement

The user is whoever runs your program

The program must wait for the user to type in something then press enter.

Try the input function, as in

mysavedinput= input ('Type some crazy thing here')

In this example the expression on the right side of the assignment operator '=' is a call to the

input function.

The prompt the user will see before they type is the quoted string between the parentheses. This

prompt is the only parameter sent into the function call

The variable to store the user typed string is on the left-hand side of the '='. The assignment operator

causes the result of the expression on the right side to be stored in the variable on the left.

The string typed by the user would be saved in the variable/storage 'mysavedinput'

3. Call a function to print a message to the screen directing the user to type in their last name

4. Read in the last name and store it in its own variable

This should be possible with similar code to the above

5. Compute the length of the users first name and store this number in a variable.

The length is the number of characters in the string representing the name.

Try using the len function as in

len('hello')

to see how it returns the length of the sequence 'passed in' between the parentheses.

The statement

mylength=len('hello')

would save the length of the string 'hello' in the variable mylength. If you had a string saved

in mystring, you could find the length of it with

mystringlen = len(mystring)

Look in text Appendix A for string or sequence operations (functions) to find the len function.

Also note the built-in functions in the online python documentation

https://docs.python.org/3/library/functions.html

Become comfortable using the documentation to find out about any functions or parts of the python

language needed. The search tool is useful

A function call accepts the input, processes it with the statements in the function definition,

2

then typically returns a result.

We use many functions in python, and this one is one of many that are defined in the standard Python

libraries (so you can use them without defining anything yourself)

6. Compute the length of the users last name and store this number in a variable.

This should be possible with similar code to the above

7. Compute the length of your (you as the programmer) first name

For your first name, you can use a literal string constant like 'Bill'

To find the length try e.g.

myfnlen = len('Monty')

The string is passed into the len function which computes the length and returns it to be

stored in myfnlen

8. Compute the minimum of the length of the user's first name and your first name and store it in a

Variable

Try Python's built in min function, as in

mymin = min(len('Monty'), len('Python'))

which would first call len to find the lengths of the first name and last name, then call the

min function to determine the smaller of them, then the result from min would be stored in mymin.

If you had already stored the lengths in fnlen and lnlen, you could use

mymin = min (fnlen, lnlen)

9. Call a function to print out an informative message showing the minimum value you computed

Try the print function, as in

Print ('Monty says the value is , myval)

10. Finally, write a definite loop which prints out each character in the user's first name (that was read

in previously), each character on its own line

A definite loop runs the block of code statements the same number of times as the length of the

sequence after the keyword 'in'.

3

The header line of the loop is written like

for x in 'Monty':

The lines after the header line can be any block of Python statements (don't leave it empty)

What happens in each run of the loop is:

(1) The next member of the sequence in order is stored in x

(2) The statements below the header line run (with access to x).

The letter 'M' would be the first letter stored in x in this example.

Try the first loop in dloop.py which will print out each character in the name (on its own line, since

there is a separate call to the print function for each character)

dloop.py:

Listing 1: dloop.py

#print out each cha r a c t e r in 'Monty '

for cha r a c t e r in 'Monty ':

print (cha r a c t e r)

lastName = 'Python '

#Print out each character in the string stored

# in the variable lastName

#Less typing, and the sequence is

# stored in a variable

for c in lastName:

print (c)

#d e c l a r e (d e c l a r e the name) and initialize (set the starting values) a list

#square brackets mean it will be a list

# the elements to store must be comma separated

mylist = [ 5, 6, 7]

#the sequence is stored in a list.

#The members of the sequence are numbers

for n in mylist:

print (n)

6 Sample Runs (what the output of the running the program

might look like)

6.1 Sample Run 1

Programmer's first name 'Monty':

Enter your first name

Monty

Enter your last name

Python

The shortest of our first names is 5 characters long

4

M

o

n

t

y

End of Sample Runs

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!