dimanche 8 juillet 2018

If loop does not take correct judgement based on varible type

In the code below, if loop does not take the condition (of true) and instead goes to the elif statement. I am trying to use if statement to control what could go into a list and what can not :

average = []

def judge(result):
    try:
        float(result)
        return True
    except ValueError:
        return 'please type number'

list_in = input('Type in your number,type y when finished.\n')
judge_result = judge(list_in)
if judge_result:
    aver_trac = aver_trac + 1
    average.append(list_in)
    print('success')
elif isinstance(judge_result, str):
    print(judge_result)

However if I specify

if judge_result == True:

then this if loop will work

Aucun commentaire:

Enregistrer un commentaire