0

I am new to python and I am alright at programming but I am not an expert. I am trying to create a bit of code that will do something based on some user input. There is nothing to specific I want it to do except to be able to do something for every option available. Here is what I have so far:

def main():
C = 0
while C != 1 or 2 or 3:
    C = (input(" "))
    if (int(C)) == 1:            
        function1() 
    elif (int(C)) == 2:         
        function2()
    elif (int(C)) == 3:        
        function3() 
    else:
        print("Invalid Choice ")

The problem I have here is that it does what I want, and for every situation I have available, except for when C is left blank. How would I catch this? I have seen many other questions on this topic but all of them are using something called raw_input in there input. I am not using it obviously. Do I just need to simply add something, should I rework my coding, if so how? Thank you very much.

EdgeAce
  • 1
  • 1
  • The conditions you are setting for your `while` are not doing what you think they are doing. – idjaw Oct 16 '16 at 23:05
  • What do you mean? – EdgeAce Oct 16 '16 at 23:06
  • @EdgeAce Please read the duplicate that was just flagged. It is an excellent explanation. – idjaw Oct 16 '16 at 23:09
  • Can someone remove the absurd duplicate ? The question is about the `input` function not about the goofy while loop ... – jadsq Oct 16 '16 at 23:14
  • @jadsq Anyway we still lack a bit of information. Does it fail because it was tried after the first input? Is it that it doesn't print `"Invalid Choice"`? – OliPro007 Oct 16 '16 at 23:16
  • @idjaw Yes thank you that helps a lot – EdgeAce Oct 16 '16 at 23:17
  • @EdgeAce Cheers. Good luck. – idjaw Oct 16 '16 at 23:17
  • @OliPro007 No multiple inputs work fine with it, I just wanted it to say something if the user were to just hit the enter key and leave the input blank. – EdgeAce Oct 16 '16 at 23:19
  • @OliPro007 Tags mentions Try-Catch so some Exception looming around. I would assume that type of trouble : `>>> a=input(" ") Traceback (most recent call last): File "", line 1, in File "", line 0 ^ SyntaxError: unexpected EOF while parsing >>> ` – jadsq Oct 16 '16 at 23:19
  • @EdgeAce This question/answer should help you fine tune your code the way you are looking to handle all input types by using exception handling. It has a lot of useful information -> http://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response – idjaw Oct 16 '16 at 23:20
  • @EdgeAce If the exception I mentionned above is your problem, then please mention it in the question so that it may be reopened for a proper answer. – jadsq Oct 16 '16 at 23:22
  • @jadsq That will come up for Python 2. I'm fairly certain that OP is using Python 3 where the error will actually be `ValueError: invalid literal for int() with base 10: ''` – idjaw Oct 16 '16 at 23:22
  • If the issue is with asking a user for valid input and handling exceptions around that, then it is again a duplicate of the link I posted in my previous comment. At this point it is best if the OP looks at the link I posted and uses that to help solve their problem. There is no point re-opening to close as a dupe again. – idjaw Oct 16 '16 at 23:23
  • @idjaw You have a very valid point for python2 vs 3. Anyway I feel that in that state, the question won't be valuable to anyone that may reach it; so it essentialy has negative value as it only brings noise to SO ... Anyway that's enough arguing for this, maybe I'll check meta's opinion on this someday . – jadsq Oct 16 '16 at 23:36
  • @jadsq fwiw I was never arguing. :) I can't remember the rep requirement but if you are able to vote for reopen, by all means please go ahead. TBH the nature of the question would be a dupe for the answer about handling inputs instead of comparing multiple values. – idjaw Oct 16 '16 at 23:46

0 Answers0