-3

I am building a function, but the function doesn't work the way it should.

def correct_number():
  wPlayer = input("Choose a number. ")
  while wPlayer is not int:
    notint = 0
    try:
      wPlayer = int(wPlayer)
    except ValueError:
      print("You didn't enter a number.")
      wPlayer = input("Choose a number. ")
      notint = 1
    if (notint != 1):
      if wPlayer <= 0:
        print('Please enter a number bigger than zero.')
        wPlayer = input("Choose a number. ")
  wPlayer += 12
  if wPlayer <= 82:
    wPlayer = 82
    print("Your number is now 82.")
  try:
    num = 12 - wPlayer
  if num < -70:
    wPlayer -= num
  elif num > -70:
    wPlayer -= 1
  print("Your number is now", wPlayer)
correct_number()
print('The Program Has Ended.')

When I run this program, and I meet the while statement's requirements, it doesn't print anything else, even the print right below the function call. Why is this? What am I doing wrong?

PythonNerd
  • 293
  • 1
  • 11
  • This code is incredibly bizzare. Why are you using `try` here in the bottom half of your code? Why are you doing `except num < -70:` (TIL that that's even legal). And literally noting prints out? At the very least, `'The Program Has Ended.'` must. – Carcigenicate Dec 06 '19 at 18:04
  • I was extremely bored and started screwing around. What does TIL mean? – PythonNerd Dec 06 '19 at 18:05
  • 2
    Wait, the `while` loop never ends. `while wPlayer is not int` will never be false unless you assign `int` literally to `wPlayer`. – Carcigenicate Dec 06 '19 at 18:06
  • "Today I learned". Reddit slang – Carcigenicate Dec 06 '19 at 18:06
  • Oh...I see. Would `while not wPlayer is int:` work? Edit - No it does not. – PythonNerd Dec 06 '19 at 18:07
  • 3
    No. `is` checks if one object is literally another object. `wPlayer is int` checks if `wPlayer` is literally the `int` class, which it never will be unless you do `wPlayer = int` at some point. You want `while not isinstance(wPlayer, int)`. That will check if `wPlayer` is a type `int` instead of type `str`. – Carcigenicate Dec 06 '19 at 18:08
  • If your objective is to test whether the user entered a number, use the built-in string method `.isdigit()`, like so: `while not wPlayer.isdigit()`. – John Gordon Dec 06 '19 at 18:09
  • 1
    This isn't a dupe, but read [this](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response). You're doing the input validation in a very overly complicated way. – Carcigenicate Dec 06 '19 at 18:11
  • Both of these suggestions work. Thanks, @Carcigenicate and JohnGordon. I would answer the question so it doesn't come up for those looking for unanswered questions. – PythonNerd Dec 06 '19 at 18:11
  • I am a beginner. That was an on topic, good question for someone at my level. – PythonNerd Dec 06 '19 at 18:20

1 Answers1

2

There's an issue with the condition in your while statement:

while wPlayer is not int:

Are you trying to check if the wPlayer variable is of type int? If so, this isn't the way to do that type check.

What you want is:

while not isinstance(wPlayer, int):

The program runs to the end with that change.

However, I should also mention that the following isn't correct Python:

 try:
    num = 12 - wPlayer
  except num < -70:
    wPlayer -= num
  except num > -70:
    wPlayer -= 1

The execution won't enter those except blocks. except is used to catch Exception objects when they're raised, such as when you try to access list index 100 in a list that has 2 items:

my_list = ["pizza", "ice_cream"]

try:
    index_one_hundred = my_list[100]
except IndexError:
    print("Whoops, you got an IndexError!")

Those except blocks should just be if and elif blocks (and, therefore, you don't need the try block either):

  num = 12 - wPlayer
  if num < -70:
    wPlayer -= num
  elif num > -70:
    wPlayer -= 1
Lightning
  • 386
  • 3
  • 15
  • Oh....ok. @Carcigencate it turns out that that wasn't legal python. – PythonNerd Dec 06 '19 at 18:17
  • When I tested it, it didn't actually work as @PythonNerd seems to have intended, though - so it seems like it's allowed, but it's essentially dead code. – Lightning Dec 06 '19 at 18:45
  • Oh, nvm, it does cause an error if you attempt to run it `TypeError: catching classes that do not inherit from BaseException is not allowed`. – Carcigenicate Dec 06 '19 at 18:50