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?

10
  • What is the exact error that you get? Your final print is not indented, but I assume that's just a copy paste problem? Commented Mar 21, 2016 at 16:25
  • 2
    Indentation is missing Commented Mar 21, 2016 at 16:26
  • Your indentation does not seem right in your else statement. Commented Mar 21, 2016 at 16:26
  • The last line in the error message is ValueError: invalid literal for int() with base 10: '' Commented Mar 21, 2016 at 16:26
  • 1
    Please edit your indentation to match exactly with your code. I think it is important for Python language Commented Mar 21, 2016 at 16:28

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

5 Comments

Okay thank you I will check if this works! Never head of a try clause, this is my third day teaching myself lol
while this works it is effectively recursive a while loop is a better option in this case
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 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.
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?
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()

Comments

Your Answer

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.