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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!