0

I am totally going insane over this function I wrote, the while loop wont work for the love of god

 while (something something):
 print("at anytime enter 0 for both origin and destination to exit the game")
 o=input("Enter origin stool's index (Must be int) ")
 d=input("Enter destination stool's index (Must be int) ")
 if isinstance(o,int) and isinstance(d,int):#if input d,o are int
 #do bla bla
 elif o==0 and d==0:#to exit manually
 print("Exiting Manually...")
 break
 elif not (isinstance(o,int) and isinstance(d,int)):
 print ("Invalid entry, exiting...")
 break 

The code should exit when o AND d are 0, and it should also exit when o OR d is not an int

However it seems that it doesn't matter what I enter, (i tried 0,0 and 0,1) it returns invalid entry, exiting...

Whats wrong with the conditions?

Henry Keiter
17.3k8 gold badges53 silver badges85 bronze badges
asked Feb 23, 2014 at 17:12

2 Answers 2

4

In Python 3, the input function always returns a string. It doesn't eval the user input like it used to do in Python 2. As a result, o and d are both strings, not integers. You need to wrap your input calls with int(input('...')) in order to turn them into ints, so your conditionals have a chance of success.

In this case, if the user inputs values that aren't numbers, the int call should fail with a ValueError. The standard way to deal with this is to wrap any calls to user input with a try-except block, to deal with users who provide bad input. So the relevant part of your code might look something like this:

try:
 o=int(input("Enter origin stool's index (Must be int) "))
 d=int(input("Enter destination stool's index (Must be int) "))
except ValueError:
 print("Invalid entry, exiting...")
 break

As a side note, your logic in the conditionals is kind of weird/broken. Your first conditional checks whether a and d are both ints, and then does processing. Then your next conditional (the first elif block) tries to compare those to 0, which will always fail since if you get to that test, that means the first conditional failed, so they weren't both ints and therefore can never be equal to 0.

answered Feb 23, 2014 at 17:15
Sign up to request clarification or add additional context in comments.

3 Comments

Sorry I made some typos with the variables, and also I updated my code so that the first if. I do have one more problem though, my 2nd condition is when it checks for if the variables are not integers, how can I do that if I had to cast the input into int initially?
@Nick Editing your post for typos is fine, but please don't edit answers into your question; it invalidates the answers. I've edited my answer to make it clearer how to deal with invalid input.
Sorry, I am pretty inexperienced. Will keep that in mind for the future. Thanks for you help it worked great!
0

Your first "if" will pass when o and d are ints regardless of their values. Only if that condition fails will it continue through to the next "elif".

Try something more like:

if isinstance(o,int) and isinstance(d,int) and (o != 0 or d != 0):
 #Conditions met, carry on
else:
 break
answered Feb 23, 2014 at 17:21

1 Comment

Thanks for the headsup! I noticed that as well after I posted my question :P

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.