Computer Science 220L Laboratory 10 – while Loops

$25.00

Category: You will Instantly receive a download link for .zip solution file upon Payment

Description

5/5 - (5 votes)

Learning objectives:

  • Develop while control structures.

 

Do NOT use for loops in this lab. Use while loops.  Notice that lab10.py is virtually an empty file.  In this lab, you will write the function headers and a main to test the functions on your own.  Use only one main() to test all functions.

 

Demonstrate each exercise for an instructor as you complete it.

 

  1. Writing functions and conditionals
  2. Write a function calculateSum(value, numIterations)à number that accepts a float, value, and an integer, numIterations. This function should add value to itself numIterations of times.  Make sure to use a while loop.  (E.g., for the call calculateSum(3, 5) the function should calculate and return 3 + 3 + 3 + 3 + 3 or 15.  Assume that the multiplication operator does not work.)

 

Add code to main() to test calculateSum().

 

  1. Write a function areEqual(num1, num2) à Boolean that accepts two float values and returns a Boolean value based on their equality.

 

Add code to main() to:

  1. Call calculateSum(.1, 10) storing the answer into the variable result.
  2. Execute areEqual(1.0, result) and stores the answer into a variable called  equals.
  3. Print “The two numbers are equal” or “The two numbers are NOT equal” based on the value of equals.

 

 

  1. Forcing good input

Often a program needs an input within a certain range of values. Now that we have indefinite loops we can force the user to enter a value in the correct range. Simply ask for the input again and again until it is in the correct range. Note: It is very important to tell the user why you are asking for the input again. Otherwise repeated “this is wrong” messages are annoying and frustrating for the user of the program.

 

Write a function called goodInput()à number that asks for a number to be input.  The function should accept as good input any number that is inclusively in the range 1 to 10, equals -1 or is greater than 50. If the input is outside of these, ask for input again until a proper value is entered.  The function should return the “good input” received by the user.      Add code to main() to test goodInput().

 

  1. Counting the digits in a number

Write a void function called numDigits(). This function repeatedly asks the user to input a positive integer. End the function when a zero or negative number is entered.

 

For each number entered, the function should print the number of digits found in that number. You could easily answer this question by reading the number as a string and using len(number), but that wouldn’t teach you about while loops.

 

Integer divide the number repeatedly by 10 until the number reaches zero, and count the number of divisions. That number is the number of digits.

 

Add code to main() to test numDigits().

 

 

  1. High-Low game

Write a void function called hiLoGame() that provides a number-guessing game.

 

Generate a random number between 1 and 100 to be guessed. Write a loop that allows the user seven guesses. For each guess, the game tells the user if the guess was “correct”, “too high”, or “too low.” The game continues until the user guesses correctly or has guessed incorrectly seven times. At the end of the game, one of the following messages is displayed: “You win in # guesses!” or “Sorry, you lose. The number was ##.”

 

To generate a random number, put from random import randint at the top of the program. Then, for example, a call to the function randint(1,10) generates a pseudo-random number in the range [1..10], i.e., including both 1 and 10.

 

Add code to main() to test hiLoGame().

 

Upload the file (don’t forget to submit).