Question
You will add to the program you created last week. This week you will add a list to keep track of all the numbers guessed
You will add to the program you created last week. This week you will add a list to keep track of all the numbers guessed and modularize your code. Be sure to import random at the beginning of your code and use a comment block explaining what your program does. Please use the following code to ADD to it (You picked the following number:) This is should be displayed after (You guessed it in ___ attempts)
import random
import sys
print('Welcome to my Guess the number program!')
print()
while (True):
print('1. You guess the number')
print('2. You type a number and see if the computer can guess it')
print('3. Exit')
print()
option=int(input('What is your choice:'))
print()
if(option ==1):
number=random.randint(1, 10)
count=0
while (True):
try:
guess=int(input('Please guess a number between 1 and 10: '))
print()
count = count + 1
if guess < 0:
continue
elif guess < number:
print('Too low')
print()
elif guess > number:
print('Too high')
print()
elif guess == number:
break
except:
print('Numbers only')
print()
print('You guessed it! It took you', count, 'attempts')
print()
if(option ==2):
print()
number=int(input('Please enter a number between 1 and 10 for the computer to guess:'))
print()
count=1
while True :
randomval=random.randint(1, 10)
if (number print('The computer guessed',randomval,'which is too low') print() count=count+1 elif (number>randomval): print('The computer guessed',randomval,'which is too high') print() count=count+1 elif (number==randomval): break print('The computer guessed it! It took',count,'attempts.') print() if(option==3): print('Thank you for playing the guess the number game!') sys.exit(0)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started