Harry Raffo
Harry Raffo

Reputation: 23

Issue with checking ranges in if statement

I'm new to python and doing a cyber sec. course, one of the modules is Python.

I am playing around trying to code a number guesser game without a tutorial. Can someone look at this code and tell me why the code doesn't seem to be following the if statements.

Thanks.

EDIT: ERROR: I am trying to create code that will judge which "difficulty" the player has selected by checking which of the ranges it fits into. The issue is, it is printing each of the "difficulty" options, not just the one it fits in to.

EDIT 2: Changed title to be more specific so it may help others.


print("Welcome to the number guesser game!")

tutorial = input("Would you like a tutorial? (Y/N): ")

while tutorial != "Y" or "y" or "N" or "n":
      input("Input not recognised, would you like a tutorial? Y (for yes) or N (for no)")
      if tutorial == "N" or "n":
         print("Let's get right into it!")
         if tutorial == "Y" or "y":
            print("In this game you will pick a number, the number you pick will be added to a random number between 1 and 20 /nYou will then attempt to guess the number generated, with a hint given every guess")
            break


difficulty = input("Please choose a random number \nGuide:\nEasy 1-10\nMedium 1-100\nHard 1-1,000\nExtreme Numbers above 1,000-10,000")


if difficulty >="1" <="10":
   print("You have chosen easy! You must be a noob!")
   if difficulty >"10" <="100":
      print("You have chosen medium! Good choice!")
      if difficulty >"100" <="1000":
         print("You have chosen hard! Pfft, good luck")
         if difficulty >"1000" <="10000":
            print("You have chosen extreme! You must be nuts")
            if difficulty <="0" >"10000":
               difficulty = input("Nice try, pick again: ")
            else:
                 difficulty = input("Input not recognised, please input a number greater than 0: ")```

Upvotes: 1

Views: 78

Answers (3)

ppwater
ppwater

Reputation: 2277

Explanation

That's because first, if the input is not y, then it will say not recognized. And it's because, if it's y, it checks the no statement, then it breaks. That means, It will say both.

Then, it goes strange because you are doing >= with str and str. It will react strange. so you should compare int with int.

Solution

First, use and in the first statement. then, use elif. and use break in the if statement too. and do int(input()) to convert to int. then, you can use elif again. and remove the quotes.

Try this:

print("Welcome to the number guesser game!")

tutorial = input("Would you like a tutorial? (Y/N): ")
while True:
      if tutorial != "Y" and tutorial !="y" and tutorial !="N" and tutorial !="n":
          tutorial = input("Input not recognised, would you like a tutorial? Y (for yes) or N (for no)")
      if tutorial == "N" or tutorial =="n":
         print("Let's get right into it!")
         break
      elif tutorial == "Y" or tutorial == "y":
         print("In this game you will pick a number, the number you pick will be added to a random number between 1 and 20 /nYou will then attempt to guess the number generated, with a hint given every guess")
         break


difficulty = int(input("Please choose a random number \nGuide:\nEasy 1-10\nMedium 1-100\nHard 1-1,000\nExtreme Numbers above 1,000-10,000"))


if difficulty >=1 and difficulty <=10:
    print("You have chosen easy! You must be a noob!")
elif difficulty >10 and difficulty<=100:
    print("You have chosen medium! Good choice!")
elif difficulty >100 and difficulty<=1000:
    print("You have chosen hard! Pfft, good luck")
elif difficulty >1000 and difficulty<=10000:
    print("You have chosen extreme! You must be nuts")
elif difficulty <=0 and difficulty >10000:
    difficulty = input("Nice try, pick again: ")
else:
    difficulty = input("Input not recognised, please input a number greater than 0: ")

And for the while loop, turn it to while True: then check it with a if statement. then you can check y or n.

Upvotes: 3

Caius Jard
Caius Jard

Reputation: 74625

Simplify your checking, and do it in a loop so they cannot progress if they enter invalid input:

difficulty = -1
while difficulty < 1 or difficulty > 10000

  try
    difficulty = int(input("Please choose a difficulty.\nGuide:\nEasy 1-10\nMedium 1-100\nHard 1-1000\nExtreme 1000-10000\n: "))
  except ValueError:
    print("not a number")

  if difficulty > 0 and difficulty <=10:
    print("You have chosen easy! You must be a noob!")
  elif difficulty<=100:
    print("You have chosen medium! Good choice!")
  elif difficulty<=1000:
    print("You have chosen hard! Pfft, good luck")
  elif difficulty<=10000:
    print("You have chosen extreme! You must be nuts")

You don't need each elif to make sure the number is greater than the end of the range of the check before; if it wasnt then that elif would have been true, not this one. In other words, if youre playing a guessing game with another human and you say "is the number less than or equal to 10" and they say no, you don't need to say "is it greater than 10 and less than.. " because the "greater than" is implied - it has to be greater than because it isn't less than or equal to

Upvotes: 1

Shrikantha Budya
Shrikantha Budya

Reputation: 644

First off in the while loop to check if the variable tutorial is "Y" or "y" or "n" or "N" you're doing tutorial != "Y" or "y" or "N" or "n", which is incorrect because it's saying if (tutorial != "Y") or ("y") or ("n") or ("N") and the latter 3 conditions return true because it is a non empty string.

with the if conditions there are 3 problems, first one is the one I just described, second is you're not converting the input to integer before checking, third is that you're nesting the conditions unnecessarily.

Upvotes: 1

Related Questions