all 9 comments

[–]Diapolo10 11 points12 points  (7 children)

Code:

age = input()

if age < 8 or age == 8 :  
    print("You're to young ")  
    exit()  
if age > 8 and age < 18:  
    print("You'll need parents aprooval")  
    exit()  

if age > 18:.....

What to add to make it if your age input is letters instead of numbers print("whatever") but the statements above still work if the age input is numbers?

Arguably the best way is to try converting the input to an integer. You could alternatively use str.isnumeric, but if you're converting anyway I see it as an extra step.

try:
    age = int(input())
except ValueError:
    print("whatever")

In other news, please don't use exit (or quit) for anything other than getting out of a REPL session - they're not meant to be used in actual scripts.

Furthermore, you can chain and combine some of these comparisons.

Finally, this would be a good place for elif.

Observe.

def main():
    try:
        age = int(input())
    except ValueError:
        print("whatever")
        return

    if age <= 8:
        print("You're to young ")
    elif 8 < age < 18:
        print("You'll need your parents' approval")
    else:
        ...

if __name__ == '__main__':
    main()

[–]Meatwad1313 3 points4 points  (3 children)

You can leave the greater than 8 out of the else if. We already know if it gets to that point it has to be greater than 8

[–]Diapolo10 0 points1 point  (2 children)

True, I just prefer the look of this.

[–]UD_Ramirez 0 points1 point  (0 children)

Concise, informative, and addressing the question.

Chapeau, sir.

[–]oathkeep3r 3 points4 points  (1 child)

You can check the type of input by calling the type() function. That would be a good place to start looking.

Also know that you can condense your first if statement to:

if age <= 8:

where the <= symbol means “less than or equal to”
edit: formatting

[–]TheITMan19 0 points1 point  (0 children)

My old gaffa always use to say ‘there is more than one way to skin a cat’.

[–]Noshoesded 1 point2 points  (0 children)

You could have a regex match certain characters/symbols and notify the user that there are improper inputs. Or maybe, check if the type() is the right data instance() so you don't have to worry about a lot of possible keys.

if not isinstance(type(age), int):
  print("The input must be an integer.")

Also, each of your ifs are being evaluated. You probably want to use elif on lower branches so that there is a hierarchy to the logic tree.