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.
'''
Python version : 2.7
Python program to implement the guessing game
'''
import random
# generate a random number between [1,100]
num = random.randint(1,100)
attempts = 0
print("Welcome to the guessing game!")
# loop that continues till the user guesses the number
while True:
# input of the guess
guess = int(raw_input('Enter your guess : '))
attempts += 1
# check if guess is correct, break from the loop
if guess == num:
break
elif guess < num: # if guess < num, print 'Too low'
print('Too low')
else: # if guess > num, print 'Too high'
print('Too high')
# print the number of attempts it took to guess the number
print('Congratulations!! You guessed the number in %d attempts '%(attempts))
#end of program
Code Screenshot:
Output:
// Pseudocode to guess the number
Declaration
number guess, num;
number attempts
Start
num = generate a random number between 1 and 100
(inclusive) ;
attempts = 0 ; // initialize attempts to 0
Input guess; //input the guess value from user
attempts = attempts + 1; // increment the
attempts
// loop that continues till the user has guessed the
number
while guess != num
do
// check if guess is low or
high
if guess > num then
print('Too
high');
else
print('Too
low');
end if
Input guess; //input the guess
value from user
attempts += 1;// increment the
attempts
end while
// output the number of attempts it took the user to
guess the number
print("Congratulations!! You guessed the number in
",attempts," attempts ")
End
Flowchart:
Get Answers For Free
Most questions answered within 1 hours.