Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In Python, note the following code: # This is a guess the number game. import random #print('Hello! What is your name?') myName = input('Hello! What

In Python, note the following code:

# This is a guess the number game. import random

#print('Hello! What is your name?') myName = input('Hello! What is your name?')

number = random.randint(1, 20) print('Well, ' + myName + ', I am thinking of a number between 1 and 20.')

guessesTaken = 0

while 1: #print('Take a guess.') # There are four spaces in front of print. guess = int(input()) guessesTaken = guessesTaken + 1 if guess == number: break elif guess < number: print('Your guess is too low.')

elif guess > number: print('Your guess is too high.')

guessesTaken = str(guessesTaken) print('Good job, ' + myName + '! You guessed my number in ' + guessesTaken + ' guesses!')

####################################################################

Modify the Guess a Number program above.

a) Make the number of guesses a variable. So, the program asks the users how many guesses they will need to find the number. Based on the user's input, the user will have that many guesses to find the number.

Ask the player how may times he thinks he needs and use his answer

b) End the WHILE loop without using the break statement.

How does the where loop end??!!!

1. Use a break

2. Use a condition the becomes "False" at the desired situation

c) Use if else at least once.

d) Replace the last two if statements in the original code with a function. Define your function in the beginning of the code.

Note: I pasted the two if statements below.

#################

if guess == number: guessesTaken = str(guessesTaken) print('Good job, ' + myName + '! You guessed my number in ' + guessesTaken + ' guesses!')

if guess != number: number = str(number) print('Nope. The number I was thinking of was ' + number)

#########################################################

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

Guide To Client Server Databases

Authors: Joe Salemi

2nd Edition

1562763105, 978-1562763107

More Books

Students also viewed these Databases questions

Question

Verify the statement made in the remark following Example 10.2.

Answered: 1 week ago

Question

1. Describe the power of nonverbal communication

Answered: 1 week ago