Python Forum
python newbie - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: python newbie (/thread-23474.html)



python newbie - marcush929 - Jan-01-2020

correct_number = "69"
hint_a_min = "0"
hint_a_max = "50"
hint_b_min = "70"
hint_b_max = "99"
guess = input("Guess a number between 1 and 100: ")
guess_count = 0
guess_max = 10
out_of_guesses = False

while guess != correct_number and not(out_of_guesses):
    if (hint_a_min <= guess <= hint_a_max) and (guess_count < guess_max):
        guess = input("Too cold! Try again: ")
        guess_count += 1
    elif (hint_b_min <= guess <= hint_b_max) and (guess_count < guess_max):
        guess = input("Too hot! Try again: ")
        guess_count += 1
    elif (hint_a_max < guess < hint_b_min) and (guess_count < guess_max):
        guess = input("So close! Try again: ")
        guess_count += 1
    else:
        out_of_guesses = True

if out_of_guesses:
    print("You lost! Try again next time!")
else:
    print("Congratulations!")
# error unsolved: the program still recognizes 3 digit number as a value in between 1 and 100
# it must be easy but i can't seem to find the error
# if there is a simpler way to code this exact game, i want to know too!
# Thank you in advance!


RE: python newbie - ndc85430 - Jan-01-2020

Why are your "numbers" (i.e. the values of the variables on lines 1-6) strings instead of integers?


RE: python newbie - marcush929 - Jan-01-2020

Oh right, thanks alot man, i solved it

correct_number = 69
hint_a_min = 0
hint_a_max = 50
hint_b_min = 70
hint_b_max = 99
guess = int(input("Guess a number between 1 and 100: "))
guess_count = 0
guess_max = 10
out_of_guesses = False

while guess != correct_number and not(out_of_guesses):
    if (hint_a_min <= guess <= hint_a_max) and (guess_count < guess_max):
        guess = int(input("Too cold! Try again: "))
        guess_count += 1
    elif (hint_b_min <= guess <= hint_b_max) and (guess_count < guess_max):
        guess = int(input("Too hot! Try again: "))
        guess_count += 1
    elif (hint_a_max < guess < hint_b_min) and (guess_count < guess_max):
        guess = int(input("So close! Try again: "))
        guess_count += 1
    elif (guess > hint_b_max) and (guess_count < guess_max):
        guess = int(input("Must be a number between 1 and 100: "))
        guess_count += 1
    else:
        out_of_guesses = True

if out_of_guesses:
    print("You lost! Try again next time!")
else:
    print("Congratulations!")