vendredi 10 février 2017

Python 3/ eli5 - Random numbers game vs computer

I have the task to make a game against the computer. I should "think of a number" and the computer must guess it. In case it has correctly guessed it, I should say C and break out of the loop. In case of a Lower number, I should say L and the computer should try to generate a lower number. H is for higher and is the opposite situation. So far I have managed to successfully implement everything with one exception. With the code below, the if I tell the computer L for example, it will not exceed the limit of the last number, however, if I then say H, it will randomly generate the numbers again. Please bear in mind this is a task for a beginners course (functions are NOT yet covered). We have to use loops. For the functions getInteger and getLetter, do not pay attention, they are functions our professor has created and are similar to input() but just restrict the user to enter something different than a letter or an integer.

Here is the code:

from pcinput import getLetter, getInteger
from random import random, randint, seed


mynum = getInteger("My number is:")


comnum = randint(0, 1000)
print("Is your number:", comnum, "?")

while True:
    answer = getLetter("That is: ")
    if answer == "C":
        print("Congratulations!")
        break
    if answer == "L":
        comnum = randint(0,comnum)
        print("Is your number:", comnum, "?")
        possibility = range(comnum, )
        continue
    elif answer == "H":
        comnum = randint(comnum, 1000)
        print("Is your number:", comnum, "?")
        continue

*comnum is the letter the computer should be entering

My question is basically how to fix this code so that the computer will create some sort of a range between the first and the last guess and do not exceed or go below it thus shortening the interval between the guesses each time. (I hope you get my point).

Thank you a lot!




Aucun commentaire:

Enregistrer un commentaire