简体   繁体   中英

The elif wont work when I try it and this wont work when it worked in my last code

year_born = input('what year were you born? ')
year = (2019)
age = (year - int(year_born))
if age <= 18:
  if age <= 65:
    print('Here is your pensioners ticket.')
  print('Here is your child's fare ticket.')
else:
  print('here is your ticket.')

I tried and elif statement on "if age <= 65:" but it shows syntax error. this works in my last code bellow. so i'm not for sure what is wrong or what I could do to make this work correctly. the problem with this is it does not initiate the "print('Here is your pensioners ticket.')". thanks for the help love you all.

first_name = input('first name only: ')
if len(first_name) >= 5:
  if first_name[-1] == 'a':
    print('you won $1000!!')
  else:
    print('you did not win $1000')
else:
  print('you did not win $1000')

The error is likely at print('Here is your child's fare ticket.') because you use ' without escaping it. Possible solutions:

print('Here is your child\'s fare ticket.')

Or

print("Here is your child's fare ticket.")

The way I would approach it:

year_born = input('what year were you born? ')
year = 2019
age = (year - int(year_born))
if age <= 18:
    print('Here is your child\'s fare ticket.')
elif age >= 65:
    print('Here is your old person ticket.')
else:
    print('here is your general adult ticket.')

From a logic standpoint you want prisoners to be 65+ and children are 18 and below. Otherwise they get a regular ticket. If this is correct then I would use the following:

year_born = int(input('what year were you born? '))
year = 2019
age = year - year_born

if age >= 65:
    print('Here is your pensioners ticket.')
elif age <= 18:
    print('Here is your child\'s fare ticket.') #need to escape the backslash here btw
else:
    print('here is your ticket.')

As for the other code you want to check the last letter first, then see if the length is above 5:

first_name = input('first name only: ')

if first_name[-1] == 'a':
    print('you won $1000!!')
else:
  print('you did not win $1000')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM