0

building a list (help)

#building a list print("Enter number of courses, please: ") len=input() i=0 b=[] print("enter all the grades: ") while True: x=input() b.append(x) if i==int(len): break i+=1 print(b) What’s wrong With my code? It writes that there is a problem in line 7

28th Mar 2021, 10:44 PM
alex kon
alex kon - avatar
3 Respostas
+ 1
You're code asks for one more than you enter at the beggining. eg. when you enter 3, it expects 4 values. i would try a for loop, or set what i call a "loop switch". eg: for i in range(int(len)): x = input() ... #OR RUNNING = True while(RUNNING): x = input() ... if <stop condition>: RUNNING = False
28th Mar 2021, 11:27 PM
Slick
Slick - avatar
+ 1
thanks ,actually i found that the problem was in i==len ......should be i==int(len)-1: because i was compered to str value of len
30th Mar 2021, 5:19 PM
alex kon
alex kon - avatar