BYU logo Computer Science

To start this assignment, download this zip file.

Lab 3b — Input Loops

Preparation

1 minute

Download the zip file for this lab, located above. This zip file has code that you will use for this assignment. Extract the files and put them in your cs110 directory in a folder called lab3b.

Adding

10 minutes

In your lab3b folder, find the file called adding.py. In this file, write a program that does the following:

  • prints “I can add numbers for you!”
  • loop forever
    • asks the person to input two numbers
    • adds the numbers and prints “The result is: [result]”
    • asks “Would you like to do more?”
    • if the answer is ‘no’ or ‘No’, then exit, otherwise repeat

Here is an example:

I can add numbers for you!
Enter a number: 5
Enter a number: 4
The result is: 9
Would you like to do more? yes
Enter a number: 3
Enter a number: 100
The result is: 103
Would you like to do more? No

Discussion with the TA:

  • Why do you need to use int()?
  • Did you write any helper functions? Which ones?
  • Did you use break or return to exit the while loop?

Concatenating

10 minutes

In concatenating.py, write a program that does the following:

  • prints “Let’s concatenate strings!”
  • loop forever
    • asks the person to enter two strings
    • if the first string is less than the second string
      • concatenate the strings and show the result: “The result is [result]”
    • otherwise
      • prints “Sorry, [string1] is greater than [string2]. I’m quitting.”
      • exits

Here is an example:

Let's concatenate strings!
Enter a string: bath
Enter a string: room
The result is: bathroom
Enter a string: hall
Enter a string: way
The result is: hallway
Enter a string: basket
Enter a string: ball
Sorry, basket is greater than ball. I'm quitting.

Discussion with the TA:

  • Did you write any helper functions? Which ones?
  • Did you use break or return to exit the while loop?

Compliments and advice

10 minutes

Use the file complements_and_advice.py to write a program that loops forever and shows the following menu:

What would you like to do?
 1) Receive compliment
 2) Receive advice
 3) Quit
Option:
  • If a person enters 1, then the program prints a compliment and then repeats.
  • If a person enters 2, then the program prints advice and then repeats.
  • If a person enters 3, then the program prints “Goodbye!” and quits.

You can have the program print any compliment or advice you want.

Here is an example:

What would you like to do?
 1) Receive compliment
 2) Receive advice
 3) Quit
Option: 1
You write the best code.

What would you like to do?
 1) Receive compliment
 2) Receive advice
 3) Quit
Option: 2
Get 8 hours of sleep every night.

What would you like to do?
 1) Receive compliment
 2) Receive advice
 3) Quit
Option: 3
Goodbye!

Discussion with the TA:

  • Did you write any helper functions? Which ones?
  • Did you convert the menu choice to an integer or leave it as a string? (You can do it either way, but this affects your comparisons)

Grading

To finish this lab and receive a grade, take the canvas quiz.

We are providing a solution so you can check your work. Please look at this after you complete the assignment. :-)