lundi 17 juin 2019

random.randint not working within given range

I have homework in python. I need to write a program that guesses a number which the user select in the range of 0-100.

I tried to write the program with random, the program prints the random int and receives input if its to high or to low. if its to low, the program generates a number between the number it printed and 100(or if it a higher number than that is the max the code can generate) but for some reason when I enter that the number it printed is to high, it doesn't set the higher number to the maximum range.

import random
high = 100
low = 0
tries = 0
print("select a number between 0-100")
x = random.randint(low, high)
print("is it: " +str(x) +"?")
while True:
    y = input("enter high if guess is higher, enter low if guess is lower and enter correct if guess is correct\n")
    if y == "low":
        low = x
        x = random.randint(low, high)
        print("is it: " +str(x) +"?")
        tries +=1
        continue
    elif y == "high":
        hige = x
        x = random.randint(low, high)
        print("is it: " +str(x) +"?")
        tries += 1
        continue
    elif y == "correct":
        print("it took: " +str(tries) +" tries")
        c = input("enter c to close and co to continue")
        if c == "c":
            break
        elif c =="co":
            continue
    else:
        print("enter valid input:\n")
        continue

I want the maximum range would be the highest number the code generated last. thank you for helping :)




Aucun commentaire:

Enregistrer un commentaire