Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
python newbie
#1
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!
Reply
#2
Why are your "numbers" (i.e. the values of the variables on lines 1-6) strings instead of integers?
Reply
#3
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!")
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python newbie laleebee 2 1,316 May-24-2022, 01:39 PM
Last Post: laleebee
  Newbie on Python syntax rud0lp20 6 2,938 Apr-21-2020, 04:26 PM
Last Post: jefsummers
Smile Help needed. Python Newbie!, it will be fun. knightdea 3 2,623 Oct-13-2019, 08:50 AM
Last Post: perfringo
  Python Linting (newbie) LieveHeer 2 2,615 Jan-24-2019, 05:36 PM
Last Post: LieveHeer
  Python newbie is asking Marcus_Gondwe 4 3,405 Apr-04-2018, 11:08 AM
Last Post: Marcus_Gondwe
  Newbie ? Python 3 Input() jlgrunr 1 2,452 Feb-17-2018, 10:26 PM
Last Post: Gribouillis
  Python Newbie phylon 1 29,983 Jan-09-2018, 10:46 AM
Last Post: Gribouillis
  NEWBIE HELP: How to tell Python when to use a function BoaVenom18 4 5,408 Dec-17-2016, 09:00 PM
Last Post: j.crater

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020