[Tutor] Shorter way for a little program
Alan Gauld
alan.gauld at btinternet.com
Sat Aug 23 21:54:59 CEST 2014
On 23/08/14 15:16, Mimi Ou Yang wrote:
> age = input("K")
> age = int(age)
> if (age == 1) or (age == 2) or (age == 3) or (age == 4):
> print ("LOL")
> else:
> print ("K")
>
> Is there a shorter way to do this program???
Yes you could do this:
print('LOL' if int(input('K')) in (1,2,3,4) else 'K')
Its shorter, but it suffers from the usual problems of
being shorter:
1) Its much harder to debug if it goes wrong
2) Almost any modifications are much harder.
Having said that there are some valid reductions you could consider:
age = int(input("K"))
if age in (1,2,3,4): # or: if 1 <= age <= 4:
print ("LOL")
else:
print ("K")
HTH
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list