Elif在我尝试时不起作用,而在我的上一个代码中起作用时将不起作用

时间:2019-03-06 22:15:50

标签: python

"zyxwvutsrqponmlkjihgfedcba"

我尝试了elif声明“ if age <= 65:”,但显示语法错误。这在下面的代码中有效。所以我不确定什么是错的或我可以做些什么才能使它正常工作。问题是它不会启动“打印('这是您的退休人员票。')”。感谢您的帮助,爱你们所有人。

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.')

2 个答案:

答案 0 :(得分:0)

该错误很可能是print('Here is your child's fare ticket.'),因为您在使用'时没有转义。可能的解决方案:

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

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

我的处理方式:

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.')

答案 1 :(得分:0)

从逻辑的角度来看,您希望囚犯年龄在65岁以上,孩子在18岁以下。否则,他们会得到一张普通票。如果这是正确的,那么我将使用以下内容:

first_name = input('first name only: ')

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

对于其他要首先检查最后一个字母的代码,然后查看长度是否大于5:

Invoke-Expression