Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Create the starting point code for this lab: def main(): name = 'Jack' rating = 3 laugh = 'Bwa' + 'ha' * rating print('My name

Create the starting point code for this lab:

def main(): name = 'Jack' rating = 3 laugh = 'Bwa' + 'ha' * rating print('My name is %s. %s.' % (name, laugh)) # call main main()

What does this code do? See if you can figure it out before you type it in and run it.

Preliminary Work

Before we get started with the lab itself, let's update this so that the name and rating values are from the user.

Getting name from the user

The starting point code hard-codes the value 'Jack' for the variable name.

name = 'Jack'

Let's replace this with hard-coded value with a question for the user. We need a string here, so we'll use input.

name = input('Enter the name: ')

Getting rating from the user

The starting point code hard-codes the value 3 in for rating.

rating = 3

Let's replace this with a query to the user. The value needs to numeric, so, we'll use the input function with the int function.

rating = int(input('Enter the evilness rating: '))

This code works sort of. It isn't resilient to user input errors. What happens if the user just presses enter in response to the first question? in response to the second question?

Minus Version

For the minus, we'll add some simple input validation to handle both of these issues.

Validate name

If the user doesn't enter anything when prompted for the name, then output sentence is goofy.

My name is .

After getting the data from the user, we should check to make sure that there is something there. We could do this by using an if statement or a while loop after the call to input. Both of these statements are controlled by a Boolean expression. We're checking for errors, so we want to know if name in an empty string. That is, the error occurs if the name is an empty string.

Now, which statement should we use? The while loop gives the user the chance to try again. So, let's use that.

while name == '': # user input error

What should we do in the block? Report the error. We can use a simple print statement.

print('Error in input. Name cannot be blank.')

It is also very important to ask the user again for a name. Otherwise nothing will change, and the program will get stuck reporting the error, with no way to fix it.

name = input('Try again. Enter the name: ')

Notice that both of these statements need to go within the block of the while loop.

After validating the input for name, continue by asking for the rating.

Validate rating

To get the rating value, we're currently using the input function. This function tries to evaluate what the user typed in as an int to get a numerical value from it. However, this makes the function more "fragile".

What does that mean? Well, what happens in the user inputs something that's not numeric like 'cat'? The cast to int will crash.

How can we fix this problem?

The input function isn't having the problem, it's the int conversion function that's unhappy. So, let's read in a string and then check the string to make sure it's ok. Remember this is called input validation. We're checking to make sure that what the user typed is a valid integer value. After we have confirmed that it's ok, we still need to have a numeric value (an int value, actually) for the rest of the algorithm.

Let's start by changing the code to read in a string.

rating = input('Enter the evilness rating: ')

Now, we can use a while loop for the check, as we did for the name. The question is: what is the error condition? We need to have a bunch of digits. In fact, we should have a non-negative integer value, so all the characters in the input string should be digits. There is a method in str named isdigit. (A method is like a function, but to use it we need a selector dot.)

rating.isdigit()

This method returns True if two conditions are met: 1) there is at least one digit in the string, and 2) all of the characters in the string are digits. Notice that these two conditions together mean that isdigit will return false if the string is empty.

Notice that the error condition is when isdigit returns False. So, this is the skeleton of the while loop.

while not rating.isdigit(): # user input error

We could have written the following, but (as you'll hear in 142), it is generally preferred to avoid comparison with Boolean literal values. [fodder: Why? (This is a hard fodder question. You can get participation points for submitting a guess, even if it's way out in left field.)]

# possible, but not as cool while rating.isdigit() == False:

So, what do we do in the block, if the user input is unacceptable? Report the error and get a new input value.

print('Error in input. Rating must be a non-negative integer.') rating = input('Try again. Enter the evilness rating: ')

When the rating value is good, that is, after the loop, we can get the numerical value for rating and calculate laugh. This can be done in two steps:

rating = int(rating) laugh = 'Bwa' + 'ha' * rating

or one:

laugh = 'Bwa' + 'ha' * int(rating)

Try it. Does it work?

Don't forget to add the opening header comments to the script, as well as descriptive comments above each function or major block of code. There is enough going on in main that there should be at least a couple of comments describing what's going on. These comments shall be above the code that they describe and shall align vertically with the code as well.

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

Step: 3

blur-text-image

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

More Books

Students also viewed these Databases questions

Question

an element of formality in the workplace between different levels;

Answered: 1 week ago