python - read file and see if number in lst are in numerical order -
the question asked me:
in book creatures of mythology, gnomes kind, bearded creatures, while goblins tend bossy , simple-minded. goblins harass gnomes making them line in groups of three, ordered length of beards. gnomes, beingness of different physical heights, vary arrangements confuse goblins. therefore, goblins must measure beards in centimeters see if lined in order. task write function assist goblins in determining whether or not gnomes lined properly, either shortest longest beard or longest shortest. input come file gnome.txt starts line containing single integer n, 0 < n < 30, number of groups process. next n lines, each containing 3 distinct positive integers less 100. each group, line saying "ordered" or "unordered" should printed: usage:
gnome() ordered unordered ordered
the within of gnome.txt
3
40 62 77
88 62 77
91 33 18
my code far
def gnome(): infile = open('gnome.txt') firstline = infile.readline() in range (int(firstline)): gnomelist = infile.readline().split() intlist = [] str in gnomelist: intlist.append(int(str)) if intlist[i] > intlist[i + 1]: print ('unordered') else: print ('ordered')
errors im getting
file "c:/users/cjakob/desktop/homework6.py", line 34, in gnome if intlist[i] > intlist[i + 1]: indexerror: list index out of range
thank much have been working on hours
you confusing indices mean. index i
represents number of sets of 3 gnomes need process, not number of gnomes in each set.
since looking check whether added value greater 1 added 2 ago, alter if statement if intlist[-2] > intlist[-1]:
. negative index represents in python element n counting end of list. note though need create sure aren't doing when there 1 element in list or throw exception because intlist[-2]
not exist!
enjoy python :)
python
No comments:
Post a Comment