0% found this document useful (0 votes)
31 views3 pages

CS 104 - W6 - Late

The document provides instructions for two programming exercises. Exercise 1 involves writing a Python program for a number guessing game that randomly selects a number within a given range and provides hints to the user. Exercise 2 involves writing a function to print a pyramid of numbers with the number of lines as input.

Uploaded by

aylgunay
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views3 pages

CS 104 - W6 - Late

The document provides instructions for two programming exercises. Exercise 1 involves writing a Python program for a number guessing game that randomly selects a number within a given range and provides hints to the user. Exercise 2 involves writing a function to print a pyramid of numbers with the number of lines as input.

Uploaded by

aylgunay
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

CS 104 – Fall 2023

Lab Exercises - Week 6


Your programs should start with the following comments: (do not forget to change “[Your
Fullname]” with your name, and “[short description]” to a sentence that highlights the task to
be performed by the program)

All of your programs should use conditionals, loops, and functions. Format your outputs as
given in the exercise.

# [Your Fullname], CS 104, LAB Week 6

# This program [Exercise 1].

Exercise 1: Write a Python program for number guess game. In this program, your computer
will randomly a choose a number within a given nonnegative range. Then, the program will
give users a hint to guess the number. For instance, if users guess is greater than the
randomly chosen number, the program will print “Decrease your guess”. if users guess is
smaller than the randomly chosen number, the program will print “Increase your guess”.
When user guesses exactly the chosen number, the program will print “Success” and print
how many steps it took to guess the correct number. If users enter -1, the program will
terminate and print “Sorry to see you left the game”.

Below is a sample run: Bold ones are the input entered by the user as part of execution.

Welcome to number guess game.

Please enter start of your range (It should ne nonnegative): 1

Please enter end of your range (It should ne nonnegative): 100

I selected a number within this range. Can you guess it? # Assume computer selected 45
Enter your guess: 56

Decrease your guess

Enter your guess: 110

Be careful. The number should be within the range!

Enter your guess: 36

Increase your guess

Enter your guess: 42

Increase your guess

Enter your guess: 48

Decrease your guess

Enter your guess: 45

Success

You guessed the number with 6 guesses.

InstrucOons:
1. Display a short welcome message to the user at the beginning of the program.

2. Prompt the customer to enter the start and end of the range. You can assume start is always
smaller or equal than end of the range. If entered range is not nonnegative, the program will
ask user to reenter the start and end of range. It will print “Range boundaries cannot be
negative! Enter start and end of your range again”.

3. You can use randint method in random library to generate a random integer. For instance,
random.randint(3,5) will randomly generate 3, 4 or 5.

4. Check if the entered number by user is within the range. If not, warn the user.

5. The program will input guesses from the user, until either number is guessed correctly or
user enters -1.

6. If users enter -1, the program will terminate and print “Sorry to see you left the game”.

7. If user predicts the number correctly, print the number of guess steps.
# This program [Exercise 2].

Exercise 2 (You can get an extra 10 points with this exercise): Implement a Python function that
prints a pyramid of numbers to the screen. The function will take the number of lines as its input. The
output (the pyramid of numbers) is given below for an input value of 4:

4321

321

21

Note: You must use nested loops to solve this question and your output must be in the exact format as
shown above to receive credit (do not print the output of the range() function at each iteration in the for
loop. You will not receive credit for that).

You might also like