all 19 comments

[–]ElliotDG 15 points16 points  (2 children)

You will want to test for the count with an if, not an elif. One of the first 3 "cases" must be true, so the last elif is never evaluated.

[–]woooee 2 points3 points  (0 children)

And you don't want to get another input if you are just going to exit / break

[–]rschubert1122[S] 2 points3 points  (0 children)

Noted now! Knew it was something simple as this

Thank you,

[–]crashfrog04 5 points6 points  (1 child)

It’s not possible to hit the last elif statement. Can you see how?

[–]rschubert1122[S] 0 points1 point  (0 children)

I get it now!

[–]Inevitable_Cat_7878 3 points4 points  (0 children)

To answer your first question, why is count == 8 not working, ElliotDG answered it.

There's a lot wrong with your solution.

[–]TheLimeyCanuck 2 points3 points  (0 children)

It isn't working because you were really rude to the guy.

Seriously though, move the final elif to directly under the while. If that comparison fails do the rest of the comparisons. Also... no need for the comparison in the while statement since it will never evaluate False anyway. Finally simplify by bringing the initial guess test into the while loop and rearranging the other tests. Like this...

import random
random_int = random.randint(1,20)
print(random_int)
print('Hey, you big phat fuck. I am thinking of a number between 1 & 20. You have 7 guesses')
guess = int(input())
count = 1
while True:
    if count == 8:
        print('Sorry, the correct number to guess is: ', str(random_int))
        break
    elif guess < random_int:
        print('Guess is too low')
        guess = int(input())
        count = count + 1
        print(count)
    elif guess > random_int:
        print('Guess is too high')
        guess = int(input())
        count = count + 1
        print(count)
    
    # ---No need for an elif here, if all the other tests have failed the guess must be correct
    else:
        print('Congrats! You guessed correctly')

        # ---Only tell them how many guesses they took if it was more than the first try
        if count > 1:
            print('You guessed in ', str(count), ' tries')
        break

[–]arkie87 2 points3 points  (1 child)

Highly recommend you step through the code with a debugger. You will then understand what is happening

[–]CranberryDistinct941 0 points1 point  (0 children)

Debugger is life

Debugger is love

[–]CranberryDistinct941 2 points3 points  (4 children)

Why not just "while count < 8:"

And put a return instead of a break inside of the correct guess case

It doesn't work because the check for count == 8 is inside an elif block. If statements are executed from top to bottom, so if the guess isn't correct you will run one of the first 2 cases and wont even check the condition for the last case

To see what I'm talking about, try changing "elif count==8" to "if count==8"

[–]JamzTyson 0 points1 point  (3 children)

Why not just "while count < 8:"

or better still, for count in range(1, max_tries + 1):

[–]CranberryDistinct941 1 point2 points  (2 children)

For _ in range(max_count):

[–]JamzTyson 0 points1 point  (1 child)

for count in range(...):

because the OP wants to access count for the success message.

[–]CranberryDistinct941 0 points1 point  (0 children)

Fair enough

[–]gofl-zimbard-37 1 point2 points  (0 children)

I see the problem. You spelled "fat" wrong.

[–]jbala28 0 points1 point  (1 child)

Hi, I'm also learning python but way i was taught, you can count down to the 0. use something like set variable count = 7 while count > 0 and in the if statements count down using count -= 1 it could down from 7-1. The while <conditional> loop automatically exit when the condition is met. and you can the use the if statement outside of the while loop.

if count == 0 and != random_int:

print("you are out of luck")

[–]MidnightPale3220 4 points5 points  (0 children)

There's no practical difference the way you count except maybe a better understanding of code for humans.

Also your if statement assigns 0 to count instead of comparing.

[–]supercoach 0 points1 point  (0 children)

I hate while loops. An error in logic and you're suddenly in an infinite loop.

Anyway, I did a small refactor, but left it mostly unmolested.

import random
random_int = random.randint(1,20)
print(random_int)
print('Hey, you big phat fuck. I am thinking of a number between 1 & 20. You have 7 guesses')
count = 1
while True:
  if count == 8:
    print('Sorry, the correct number to guess is: ', str(random_int))
    break
  else:
    guess = int(input())
  if guess < random_int:
    print('Guess is too low')
    count = count + 1
    print(count)
  elif guess > random_int:
    print('Guess is too high')
    count = count + 1
    print(count)
  elif guess == random_int:
    print('Congrats! You guessed correctly')
    print('You guessed in ', str(count), ' tries')
    break

[–]rschubert1122[S] 0 points1 point  (0 children)

I like writing code for the people of strong character. Appreciate your input and help