2

Hello I am new to coding, here I wrote a simple code based on the user's input. I can get every if and elif response to work but my program fails when the input is not an integer.

def tables_bussed():
    tables = input("How many tables can you bus per hour? (I can only read numbers.)")
    if int(tables) > 80:
        print ("That\'s rediculous, you lying twit.")
        tables_bussed()
    elif int(tables) > 60:
        print ("If what you are saying is true then you have quite a talent!")
    elif int(tables) > 30:
        print ("Impressive! Yet there\'s always room for improvement, now isn\'t there.")
    elif int(tables) > 0:
        print ("How sad.")
    else:
        print ("Are you dumb or just daft.")
        tables_bussed()

tables_bussed()

am I missing something in my else clause?

nthall
  • 2,847
  • 1
  • 28
  • 36
  • What is the exact error that you get? Your final print is not indented, but I assume that's just a copy paste problem? – srowland Mar 21 '16 at 16:25
  • 2
    Indentation is missing – Sharad Mar 21 '16 at 16:26
  • Your indentation does not seem right in your else statement. – Loufylouf Mar 21 '16 at 16:26
  • The last line in the error message is ValueError: invalid literal for int() with base 10: '' – Dylan Woodie Mar 21 '16 at 16:26
  • apologies, my last print is indented in my code i didn't adjust it for the qustion – Dylan Woodie Mar 21 '16 at 16:26
  • @DylanWoodie -- From the error statement in your comment, it looks like you're accidentally passing an empty string to `int` somehow. – mgilson Mar 21 '16 at 16:28
  • 1
    Please edit your indentation to match exactly with your code. I think it is important for `Python` language – Ian Mar 21 '16 at 16:28
  • @DylanWoodie -- People are asking you to fixup the indentation (and I agree, it's a good idea). To do that, delete the code that is already in the question, copy the the code from your editor directly into the question, highlight it with the mouse and click the format code button (it looks like `{}` in the editor's toolbar). – mgilson Mar 21 '16 at 16:30
  • If you type anything other than an integer, you will get an exception when you try and cast the input to an `int`. If you don't want your program to crash, catch the exception. – khelwood Mar 21 '16 at 16:30
  • To handle only certain data types you want to handle, you could use `isinstance(x, int)`. take a look on Solution 1-2 of this post: http://stackoverflow.com/questions/35572698/python-if-not-statement-with-0-0/35572950#35572950 – Ian Mar 21 '16 at 16:31

2 Answers2

3

You need a try except clause, I don't want to redo your program, but here is the general concept

def tables_bussed():
    tables = input("How many tables can you bus per hour? (I can only read numbers.)")
    try:
        tables = int(tables)
    except ValueError:
        print ('Sorry dude, you must input a number that is an integer')
        tables_bussed()

Because I define tables as an integer in the try clause you don't have to repeatedy use the int(tables) statement, you can just test for values

So put right after you define tables (note you have some indentation problems but probably not in your code)

The program will try to resolve tables as an integer, if not successful it will prompt the user to retry

There is a lot out there about try except clauses, they are very useful for catching user input problems or other problems you might have

PyNEwbie
  • 4,882
  • 4
  • 38
  • 86
  • Okay thank you I will check if this works! Never head of a try clause, this is my third day teaching myself lol – Dylan Woodie Mar 21 '16 at 16:46
  • 2
    while this works it is effectively recursive a while loop is a better option in this case – gkusner Mar 21 '16 at 16:55
  • Why don't you explain in your answer why you think it is better instead of making a comment below my answer. An assertion is not very useful to a new programmer. You should clearly make the case. You have an example, I dislike while loops in general and so the user will get the most benefit if you help them understand what is going on. – PyNEwbie Mar 21 '16 at 16:58
  • @PyNEwbie the answer was already accepted, I simply wanted to show an alternate, however since you asked I feel that unnecessary recursion is worse than a simple loop - the fact that you "dislike while loops" seems to be a strange assertion also. A while loop in this case provides a clear way to force the input to be correct without causing any possibility of a stack overflow due to recursion. – gkusner Mar 22 '16 at 14:05
  • I am just saying that another answer is generally not helpful to us Newbie's unless their is an explanation as to why it is better, more eff. etc. I am not a programmer by training and so it is just an assertion to me that this is better.. For answers on SO to survive time they should have an explanation and not just a code dump. My explanation is not detailed "try to resolve tables as an integer, if not successful it will prompt the user to retry" When I look at your code I wonder if it is even true at the start - why can't it be false at the start? – PyNEwbie Mar 22 '16 at 14:14
1

just put it all in a while loop:

def tables_bussed():
    while True:
        tables = input("How many tables can you bus per hour? (I can only read numbers.)")
        if tables.isdigit():
            if int(tables) > 80:
                print ("That\'s rediculous, you lying twit.")
                continue
            elif int(tables) > 60:
                print ("If what you are saying is true then you have quite a talent!")
            elif int(tables) > 30:
                print ("Impressive! Yet there\'s always room for improvement, now isn\'t there.")
            elif int(tables) > 0:
                print ("How sad.")
            break
        else:
            print ("Are you dumb or just daft.")

tables_bussed()
gkusner
  • 1,244
  • 1
  • 11
  • 14