lundi 22 novembre 2021

Can't get a secondary if/else question to work in python

I am brand new to coding and I am trying to get some basics down, and I think I have made a mistake in the code below, for the second if/else statement (asking are you sure?) the if statement is coming back as always true regardless of the input.

def age_calc():
    target_year= input('What future year do you want to know your age?')
    born_year = input ('what year were you born?')
    target_year = int(target_year)
    born_year = int(born_year)
    age = target_year - born_year
    print('In the year', target_year, 'you will be', age)

question=input('Do you want to know how old you be in a certain year?').lower()
if [question.startswith('y'), 'sure', 'ok',]:
    age_calc()
else:
    y_or_n =input('Are you sure?').lower()
    if [y_or_n.startswith('y'), 'definitely', 'I am']:
        print ('ok then')
    else:
        age_calc()

This is a little frustrating as this previous version works fine:

if [question.startswith('y'), 'sure'.lower, 'ok'.lower]:
     target_year= input('What future year do you want to know your age?')
     born_year = input ('what year were you born?')
     target_year = int(target_year)
     born_year = int(born_year)
     age = target_year - born_year
     print('In the year', target_year, 'you will be', age)
else:
     print('ok then')

The if/else statement in this code works correctly so I'm guessing I've used the wrong wording in the first code.

Aucun commentaire:

Enregistrer un commentaire