0

When I use the function, def problem1_5(age), and instead of inputting a value for age, I run the code, it crashes. How can I improve the code so when I dont input any value for age, it doesn't crash

This is for an online course i am undertaking, and I've already tried putting in a if at the beginning of the code statement but it could be possible that I did it inaccurately.

def problem1_5(age):
    if age < 7:
        print ("have a glass of milk")
    elif age < 21:
        print ("have a coke")
    else:
        print ("Have a martini")

I expect, when I enter nothing, to take me to another line or maybe end the code, but it just crashes.

Austin
  • 25,759
  • 4
  • 25
  • 48

1 Answers1

0

Add a default value for the input argument.

def problem1_5(age=999):
    if age < 7:
        print ("have a glass of milk")
    elif age < 21:
        print ("have a coke")
    else:
        print ("Have a martini")

Now age is 999, unless another value is assigned to it.

Fariborz Ghavamian
  • 809
  • 2
  • 11
  • 23
  • thank you very much, just one question, why doesnt it output "have a martini" since it technically falls into the else catagory – user10289295 Jan 27 '19 at 08:58
  • It does for me! Put a print statement "print(age)" right after "def problem1_5(age=999):" and see what is the value of "age" when you call "problem1_5". – Fariborz Ghavamian Jan 27 '19 at 09:12