A201 Introduction to Programming 1

A201 Lab 3

Date: Tuesday, September 5, 2023.
Due Date: Tuesday, September 12, 2023.

Goal: to use conditionals in Python.

In this lab, we will be working on conditionals in Python.

Ex. 1.

Open the Python Idle and create a new file called lab3.py. Add your name and the lab number as a comment at the top.

Copy the following code discussed in class to the file lab3.py:

print("Enter a temperature")
temperature = float(input())

if temperature <= 32:
    print("The water is freezing")
elif temperature >= 212:
    print("The water is boiling")
else:
    print("The water is liquid")

Modify the program by printing out an outfit recommendation based on the value of the temperature, such as "Warm coat and gloves" if the temperature is less than 35. Add a couple more elif statements and set your own ranges for the temperature for different outfits.

Ex. 2. a Secret Number.

Copy the following code discuss in class to the file lab3.py:

# program letting the user guess a secret number
secretNumber = 3
guess = 0
print("\n\nTry to guess a secret number between 1 and 10")
guess = int(input())

if guess == secretNumber:
    print("What a guesser! You are correct!")
else:
    print("No, ", guess, " is incorrect.\n\n")

    print("What is your second guess?")
    guess = int(input())

    if guess == secretNumber:
        print("This time you are correct!\n")
    else:
        print("Sorry, wrong again. The secret number is")
        print(secretNumber)

Test the program to make sure it works.

b. Random number

Add the following line at the beginning of the file, just below the comment:
from random import *

Then go to the line where the secret number is declared and replace the value 3 by
randrange(1, 11)

Now the program should generate a different value for the secret number every time you run it. Try it a few times to check that it works fine.

c. Giving a hint

Modify the program so that if the user doesn't guess on the first try, you let them know whether their guess is lower of higher than the secret number. For that, in the first else: you need to add a conditional that compares the guess and the secret number and output the appropriate message. Test the program with this change to make sure it works.

Lab Submission

Upload the file lab3.py in Canvas, Assignments, Homework 3. You can wait until you finish the homework to upload both files at the same time.