Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Cannot figure out problem with random.randint(x,y)

I’m trying to write a program where you think of a number and the computer tries to guess it by going higher and lower but it refuses to work. I’ve narrowed the problem down to line 6 or 7 by trimming bits of the program in and out but I have no idea what’s wrong with those lines in order to fix it.

import random
print("Think of a number between 0 - 100\nType:\n'Higher' if the number is too low\n'Lower' if the number is too high\n'Correct' if the number is correct")
minRange = 0
maxRange = 100
while True: 
    comGuess = random.randint(minRange,maxRange)
    print("Is it " + comGuess + "?")   
    answer = input("")
    if answer == "Higher" or answer == "higher" or answer == "h":
        minRange = comGuess
    elif answer == "Lower" or answer == "lower" or answer == "l":
        maxRange = comGuess
    elif answer == "Correct" or answer == "correct" or answer == "c":
        input("Yay I won!")
        break
    else:
        print("I don't understand")

As far as I can tell it should say a number, you provide a response and it guesses again and again with ever narrowing parameters until it gets the correct answer. I’m still learning Python but the lack of error message is really stumping me, the program just opens and instantly closes. I have the input on the correct answer so the program doesn’t instantly close upon a correct answer

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

on line 7, change print("Is it " + comGuess + "?") to print("Is it " + str(comGuess) + "?"). This works because you’re trying to combine a string and an int (a number) in a print function, and that won’t work, so you have to convert the int to a str first

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading