CS 104 - W6 - Late
CS 104 - W6 - Late
All of your programs should use conditionals, loops, and functions. Format your outputs as
given in the exercise.
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.
I selected a number within this range. Can you guess it? # Assume computer selected 45
Enter your guess: 56
Success
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).