0
max = 30



name= input("what is your name")
print("hello " + name)
age = int(input("how old are you"))

if age >= min and age <= max :
    print("you just earned yourself a free holiday!")
elif age < min:
    if min - age == 1:
        print("sorry " + name + " you are not old enough, please come back in {0} year.".format(min - age) )
    else:
      print("sorry " + name + " you are not old enough, please come back in {0} years.".format(min - age) )
else:
    print("too old ")```

the following lines are so if someone has to wait 1 year the program should print "come back in 1 year" not "come back in 1 years":

        print("sorry " + name + " you are not old enough, please come back in {0} year.".format(min - age) )
    else:
      print("sorry " + name + " you are not old enough, please come back in {0} years.".format(min - age) )**

so i was just wondering if there was a simpler way to do this without adding another if statement.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • Note that Python has built in functions `max` and `min` which find the maximum and minimum of an iterable, respectively. It might be better practice to not overwrite these variable names. – M Z Jun 25 '20 at 18:40
  • `"...come back in {0} year(s)".format(min - age)` would probably be sufficient in casual usage. No conditionals necessary. – chepner Jun 25 '20 at 18:41

2 Answers2

2

Use a conditional expression (x if y else z):

print("sorry " + name + " you are not old enough, please come back in {0} {1}.".format(min - age, "year" if min - age == 1 else "years") )

Or, if your Python is recent enough to support f-strings:

print(f"sorry {name} you are not old enough, please come back in {min - age} {"years" if min - age == 1 else "year"}")
Thomas
  • 174,939
  • 50
  • 355
  • 478
0

You can use a f-string for this (or extend the format function you use already)

print(f"sorry {name}, you are not old enough, please come back in {min-age} year{'s'*int(min-age>1)}")
David Wierichs
  • 545
  • 4
  • 11