lundi 30 octobre 2017

I am trying to create a program in which the computer picks a random number between 1 and 100 and tries to guess it

I am new to Python and I am just programming some random things for fun. I want to write a program in which the computer generates a random number between 1 and 100 and then tries to guess it. I do not want the user to interfere with it in any way. I also want to count how many guesses the computer took. I am also trying to have it run multiple games and calculate the average amount of guesses. I know I need to incorporate a while loop, but I don't know how to have the computer keep guessing random numbers instead of just listing every number within the range. Can someone help?

import random

the_num = random.randint(1, 100)
print('The number to guess is',the_num)
comp_guess = random.randint(1, 100)
print('The computer guesses ', comp_guess)
tries = 1

while comp_guess != the_num:
    print('The computer guesses ', comp_guess)
    tries = tries+1
    if comp_guess > the_num:
        comp_guess = comp_guess-1
    elif comp_guess < the_num:
        comp_guess = comp_guess+1
    elif comp_guess == the_num:
        break

print(tries)
print('The computer guessed it right!')




Aucun commentaire:

Enregistrer un commentaire