Use the Python IDLE editor to create the source code for the \"numberguess.py\"
ID: 3865196 • Letter: U
Question
Use the Python IDLE editor to create the source code for the "numberguess.py" program. This program is in the "Basic Python Programming" chapter in its "An Example Python Program: Guessing a Number" section. If you mistakenly create syntax errors, find and fix them. Run the program and test it with various values. Refer to the "numberguess.py Program" document to see example output. Describe the number guessing strategy you used to guess the program's secret number in as few guesses as possible. Research the "binary search algorithm" and compare it to your guessing strategy. Look over the program to find differences between Python and Java. Describe some of the differences you find. Are they major or minor differences? Evaluate some of the differences you find. Which do you prefer and why?
Explanation / Answer
#The python program that generatea a random number
#in a range of 1 t o50. Then prompt user to guess
#and prints the guess hints and prints the number
#of guesses taken and minimum number of guesses
#using binary search
#numberguess.py
import random
import math
guessCount = 0
repeat=True
print('Welcome to guessing game')
#generate a random number in a range of 1 to 50
number = random.randint(1, 50)
print('Guess a number in a range of 1 to 50')
while repeat:
print('Take a guess.') # There are four spaces in front of print.
guess = int(input())
guessCount = guessCount + 1
if guess < number:
print('Your guess is too low.') # There are eight spaces in front of print.
if guess > number:
print('Your guess is too high.')
if guess == number:
#set repeat =false
repeat=False
#print guesse taken to guess the random number
print('Your number of guesses are '+str(guessCount))
#print minimum guesses of binary search is log(number)
minGuesses=math.floor(math.log(number))
print('The minimum number of guesses are using binary search are '+str(minGuesses))
------------------------------------------------------------------------------------------------------
Sample Output:
>>>
Welcome to guessing game
Guess a number in a range of 1 to 50
Take a guess.
25
Your guess is too low.
Take a guess.
35
Your guess is too low.
Take a guess.
40
Your guess is too high.
Take a guess.
39
Your number of guesses are 4
The minimum number of guesses are using binary search are 3
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.