Python error - name lengths -
this question has answer here:
here's requirement:
write program checks how long name is. program should take name input user.
if name has 3 or fewer letters, program should work this:
enter name: lin
hi lin, have short name.
if name has between 4 , 8 letters (inclusive), program should work this:
enter name: jimmy
hi jimmy, nice meet you.
otherwise, if name has more 8 letters, program should work this:
enter name: yaasmeena
hi yaasmeena, have long name.
here's attempt returns "hi xxxxxxx, nice meet you" if name length > 3
name = input('enter name: ') if len(name) <= 3: print ('hi',name, ', have short name.') elif len(name) >= 3: print ('hi',name, ', nice meet you.') elif len(name) > 8: print ('hi',name, ', have long name.')
why don't check bounds first?
name = input('enter name: ') if len(name) <= 3: r = 'you have short name.' elif len(name) > 8: r = 'you have long name.' else: r = 'nice meet you.' prins 'hi {}, {}'.format(name, r)
Comments
Post a Comment