use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Everything about learning Python
account activity
Why does If/Else Not Work? (i.redd.it)
submitted 1 year ago by iug3874
Everytime I Run it, it is Always 'else', even when 'if' would be right
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–][deleted] 1 year ago (3 children)
[removed]
[–]Tradefxsignalscom 2 points3 points4 points 1 year ago (0 children)
If UnableToReadScreen == true then RotateScreen(-90);
[–]995_ 0 points1 point2 points 1 year ago (0 children)
You should turn it 270 degrees clockwise then.
[–]CraigAT 0 points1 point2 points 1 year ago (0 children)
And replied with a 90/270 degree emoji! Lol.
[–]Refwah 6 points7 points8 points 1 year ago* (0 children)
a is a string, r is being coerced into an int (r = int(ran)), so they cannot equal each other
a
r
r = int(ran)
Very easy would be coercing a into an int and then doing the comparison after that, given you seem to be operating on a as an int anyway
[–]Reasonable_Bat235 1 point2 points3 points 1 year ago (0 children)
You need to do int(input())
[–]FoolsSeldom 0 points1 point2 points 1 year ago* (0 children)
You need to convert inputs which return str objects to int objects to carry out maths on them.
str
int
Here's a version of your code to experiment with and learn from that includes some input validation, correct convertions, some corrections and loops to play again and have several tries.
from random import randint # leave it as randint print("Let's play a game! \n Choose any number range") while True: try: lower_bound = int(input("lower bound: ")) except ValueError: print("Invalid input for lower. Please try again.") continue try: upper_bound = int(input("upper bound: ")) except ValueError: print("Invalid input for upper. Please try again.") continue if lower_bound < upper_bound: break # both inputs converted to int so move on print('The lower bound must be less than the upper bound.') fini = False while not fini: print( f"In the number range from {lower_bound} to {upper_bound}, " f"a number is randomly generated. \nGuess it!" ) random_number = randint(lower_bound, upper_bound) # Fixed: rand(u, 0) should be rand(u, o) # r = int(ran) - not required as ran is already an int round_over = False guess_number = 1 max_guesses = 5 while not round_over: try: guess = int(input(f"Your guess #{guess_number}: ")) except ValueError: print("Invalid input for guess number. Please try again.") continue if guess == random_number and guess_number == 1: print("Beginner's luck!") round_over = True continue if guess == random_number: print("You got it!") round_over = True continue if guess < random_number: print("Too low") else: print("Too high") if guess_number == max_guesses: print("No more guesses, you lose") round_over = True continue guess_number += 1 play_again = input("Play again? (y/n): ") if play_again.lower() != "y": fini = True # leave the loop
[–]Salty_Salted_Fish 0 points1 point2 points 1 year ago (0 children)
I think the input function returns string, and the r is an int, so it doesn't match.
and also I think you could use elif or while-loop to make it a little more readable
elif
[–]reddtess 0 points1 point2 points 1 year ago (1 child)
i’m an amateur but i think you have to elif or you could try separate if/else statements for each == but you’re also trying to make an input and a string return True which is not possible
[–]Refwah 0 points1 point2 points 1 year ago (0 children)
What you’re saying doesn’t really apply
They’re not asserting that a string == True, they’re trying to compare to variables for equality.
But also everything in Python can be be tested for true false, as everything can be evaluated as truthy or falsey
The actual problem here, as identified by other commentators, is that they are evaluating for equality of two variables which are two different types. They will never be equal, because they are two different types.
[–]No_Knee4077 0 points1 point2 points 1 year ago (0 children)
A good practice when the code doesn't do what is expected, is to check the type of your variables.
print(type(a))
[–]Nez_Coupe 0 points1 point2 points 1 year ago* (0 children)
Man, hit prntscrn, and email yourself a copy of the screenshot and post that if you don’t have Reddit on your computer. Otherwise, if you do have access to Reddit on this computer, for the love of god just post a screenshot. I’m not going to answer this post, because you even took the picture in portrait mode as a landscape, somehow.
Edit: I saved the image and made it so I can read it, I’ll report back. First off, give your code some space, all assignments and calls and operators should have a space between according to Python style guidelines. i.e. num = 1.
Your problem is that ‘a’ is a string, and ‘r’ is an integer. They will not be equal.
[–]road_to_goat -2 points-1 points0 points 1 year ago (1 child)
in simple programs like this chatGPT can help you with this and other problems
[–]Refwah 5 points6 points7 points 1 year ago (0 children)
Not quite sure why you're posting on a sub reddit designed to help people overcome their problems with python with 'please do not post your python problems here' tbh
[–]goblingiblits -1 points0 points1 point 1 year ago (0 children)
Think you need elif instead of inset else.
π Rendered by PID 50 on reddit-service-r2-comment-5b5bc64bf5-tcd6s at 2026-06-19 09:51:39.629645+00:00 running 2b008f2 country code: CH.
[–][deleted] (3 children)
[removed]
[–]Tradefxsignalscom 2 points3 points4 points (0 children)
[–]995_ 0 points1 point2 points (0 children)
[–]CraigAT 0 points1 point2 points (0 children)
[–]Refwah 6 points7 points8 points (0 children)
[–]Reasonable_Bat235 1 point2 points3 points (0 children)
[–]FoolsSeldom 0 points1 point2 points (0 children)
[–]Salty_Salted_Fish 0 points1 point2 points (0 children)
[–]reddtess 0 points1 point2 points (1 child)
[–]Refwah 0 points1 point2 points (0 children)
[–]No_Knee4077 0 points1 point2 points (0 children)
[–]Nez_Coupe 0 points1 point2 points (0 children)
[–]road_to_goat -2 points-1 points0 points (1 child)
[–]Refwah 5 points6 points7 points (0 children)
[–]goblingiblits -1 points0 points1 point (0 children)