Question

PYTHON Exercise 5. Guess the number 1. Develop a “Guess the number” game. Write a program...

PYTHON Exercise 5. Guess the number

1. Develop a “Guess the number” game. Write a program that comes up with a random number and the player has to guess it. The program output can be like this: I am thinking of a number between 1 and 20. Take a guess. 10 Your guess is too low. Take a guess. 15 Your guess is too low. Take a guess. 17 Your guess is too high. Take a guess. 16 Good job! You guessed my number in 4 guesses!

2. Complicate it by having random lower and upper bounds. Make sure your lower bound is not higher than you upper bound!

3. Now, instead of user input make the code guess it automatically. Make sure that for the automatic guesses you don’t use the number to guess.

Homework Answers

Answer #1

CODE IN PYTHON:

import random

rand = random.randrange(1,21)
guess = input("I am thinking of a number between 1 and 20. take a guess:")
guess = int(guess)
count = 0
while(True):
count +=1 ;
if(guess==rand):
print("Good job")
break;
elif(guess>rand):
guess = input("Your guess is too high. take a guess:")
guess = int(guess)
else:
guess = input("Your guess is too low. take a guess:")
guess = int(guess)
print("you guessed my number in "+str(count)+" times")

Indentation:

OUTPUT:

CODE IN PYTHON:

import random

rand = random.randrange(1,21)
print("I am thinking of a number between 1 and 20. take a guess:")
low = 1
high = 20
guess = random.randrange(low,high+1)
count = 0
while(low<=high):
count +=1 ;
print("Your guess is:",guess)
if(guess==rand):
print("Good job")
break;
elif(guess>rand):
high = guess-1
if(low<=high):
print("Your guess is too high. take a guess:")
guess = random.randrange(low,high+1)
else:
low = guess+1
if(low<=high):
print("Your guess is too low. take a guess:")
guess = random.randrange(low,high+1)
print("you guessed my number in "+str(count)+" times")

Indentation:

OUTPUT:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a program to pull a random number between 1-100 inclusive. Ask the user to guess...
Write a program to pull a random number between 1-100 inclusive. Ask the user to guess the number. If the random number is higher than the guess, print "Higher". If the random number is less than the guess, print "Lower". If the random number is equal to the quess, print "Correct!". Create a variable to count the number of guesses and intitialize it to zero. int guesses=0; Increase this variable by 1 every time the user enters a new guess....
Please make a Python program where the computer chooses a number and the player guesses the...
Please make a Python program where the computer chooses a number and the player guesses the number. And at the end, please report the number of attempts. You need an input, a random number, if statement, and a while loop. Please reference the pre-lab document in the Blackboard. Please prepare a Word document that has the Python code and the screen shot of your output. Here is a sample of what the output screen should look like. What is your...
Write a Python program that plays a number guessing game with a human user. The human...
Write a Python program that plays a number guessing game with a human user. The human user will think of a number between 1 and 100, inclusive. Then the program has to guess what the user entered. keep track of the number of interaction it takes for the computer to guess the number. sample run: enter number to be guessed:88 output: you entered 88, and it took the program 3 iterations to guess.
Write a c++ program that has the user guess a number between 1 and 100, you...
Write a c++ program that has the user guess a number between 1 and 100, you program should output correct guess if the user guess the right number, too high if they guess a number higher, too low if the guess a number lower and handle invalid input
Write a c++ program to pull a random number between 1-100 inclusive. Ask the user to...
Write a c++ program to pull a random number between 1-100 inclusive. Ask the user to guess the number. If the random number is higher than the guess, print "Higher". If the random number is less than the guess, print "Lower". If the random number is equal to the quess, print "Correct!". Create a variable to count the number of guesses and intitialize it to zero. int guesses=0; Increase this variable by 1 every time the user enters a new...
Write a script that plays a simple “guess the number” game with the user. It should...
Write a script that plays a simple “guess the number” game with the user. It should select a random integer in the range [0 - 10], ask the user to guess the value, and provide feedback of the form “too high”, “too low”, or “congratulations” as appropriate. After the correct value is guessed, the script should terminate.
Goal: Write a simple number guessing game in java. The game picks a number between bounds...
Goal: Write a simple number guessing game in java. The game picks a number between bounds input by the user, then user has to guess the number. The game will tell you if your guess is too high or too low. When you guess it, the game will tell you how many guesses it took Run multiple games and print statistics (# games, # guesses, avg) when all done. Sample Run: G U E S S I N G G...
Draw a flowchart and pseudocode that describes the process of guessing a number between 1 and...
Draw a flowchart and pseudocode that describes the process of guessing a number between 1 and 100. -Make a working version of this program in Python. ( need the correct code for program ) -After each guess, the player is told that the guess is too high or too low. -The process continues until the player guesses the correct number.
Game to guess a number %% Guess Number Game while(1) count=0; guess=input('Guess a number between 0...
Game to guess a number %% Guess Number Game while(1) count=0; guess=input('Guess a number between 0 to 10 '); if(R>guess) disp('Your guess is too small') elseif (R<guess) disp('Your guess is too large') elseif (R==guess) disp('You are correct! It is ');guess R=floor( rand()*10 ); end count=count+1; if(count==3) break; end end You need to modify this code: (1)Player fails the game if you cannot make it within 3 tries. (2)Player can do this game up to 10 times. (3)If player input is...
Write a program that plays a number guessing game with a human user. The human user...
Write a program that plays a number guessing game with a human user. The human user will think of a number between 1 and 100, inclusive. Then the program has to guess what the user entered. keep track of the number of interaction it takes for the computer to guess the number. sample run: enter number to be guessed:88 output: you entered 88, and it took the program 3 iterations to guess.
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT