Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Dental Assisting

Authors: Doni Bird, Debbie Robinson

13th Edition

978-0323624855, 0323624855

Students also viewed these Programming questions

Question

4. Discuss the strategies available to market leaders.

Answered: 1 week ago