Question
I need help in explaining this Guess the Number Program in Python? import random import sys print(Welcome to my Guess the number program!) while (True):
I need help in explaining this "Guess the Number Program" in Python?
import random
import sys
print("Welcome to my Guess the number program!")
while (True):
print("1. You guess the number")
print("2. You input a number and see if the computer can guess it")
print("3. Exit")
option=int(input("What is your choice:"))
if(option ==1):
number=random.randint(1, 10)
count=0
while (True):
try:
guess=int(input("Please guess a number between 1 and 10: "))
count = count + 1
if guess < 0:
continue
elif guess < number:
print('Too low')
elif guess > number:
print('Too high')
elif guess == number:
break
except:
print("numbers only")
print('You guessed it! It took you', count, 'attempts')
if(option ==2):
number=int(input("Please enter a number between 1 and 10 for the computer to guess:"))
count=1
while True :
randomval=random.randint(1, 10)
if (number print("The computer guessed",randomval,"which is too low") count=count+1 elif (number>randomval): print("The computer guessed",randomval,"which is too high") count=count+1 elif (number==randomval): break print("The computer guessed it! It took",count,"attempts.") 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