This while loop wont run?
Question:
I’ve been doing some basic practice exercises for python, and so far it has been quite smooth flowing and such though I’ve run into a problem where the second while loop will not run. So far I’ve re-indented, defined the whole second and tried putting parameters on the loop where done = True, while done == True:, done = False
(later on in the code was what I previously had down but I removed it in effort to get it working.)
P.S some advice on my code would be greatly appreciated if you feel it necessary, as tbh I’m just winging it as I’ve been teaching myself how to code in python. I’ve included the first while loop too, as I figured that far down the code may be cause for the problem unknowingly.
import random
Olist = []
while True:
counter = 0
counter2 = 0
print("nWelcome to exercise 05")
InH1 = str(input("nDo you wish to continue? (y/n):"))
if InH1 == 'y':
list1 = random.sample(range(30), 11)
list2 = random.sample(range(30), 9)
print("n<><>Two random lists have been generated.<><>")
print(list1)
print(list2)
max_index = len(list2)-1
print("n<><>Max index has been generated.<><>")
InD = str(input("nStart the func? (y/n):"))
if InD == 'n':
break
if InD == 'y':
while True:
if counter2 == 11:
print(Olist)
InL = str(input("nEnter any key to return to the start of the program,
or,n enter 'q' to break the program."))
if InL == 'q':
break
if list1[counter2] == list2[counter]:
Olist.append(list2[counter])
counter + 1
elif counter == 10:
counter = 0
else:
counter + 1
if InH1 == 'n':
break
Answers:
You wrote:
while True:
counter = 0
counter2 = 0
counter = 0
is OUTSIDE of the while-loop
Did you want the following?
while True:
counter = 0
counter2 = 0
if list1[counter2] == list2[counter]:
Olist.append(list2[counter])
counter + 1
elif counter == 10:
counter = 0
else:
counter + 1
I think your counter + 1
statements are causing you serious trouble. Really take a close look there… Don’t you mean counter = counter + 1
?
A few details:
- there is no action on
counter2
after you assign counter2 = 0
so you will never enter the if counter2 == 11
branch [unreachable] and – unless needed somewhere else – you can completely remove that part
- as said by Tom you have to ask the counter increment as
counter = counter + 1
or with the short form counter += 1
- you have to control also that
counter
doesn’t get over the max_index
or you will try to fetch an unexisting element in the array generating an "IndexError: list index out of range"
from the if list1[counter2] == list2[counter]
line before counter
can be reset by the following elif
branch
- you need to create a condition to stop the second loop or you will not come to an end of the program: this is why I modified it into
while counter < max_index:
: the same line will allow you to safely remove the elif
branch of the previous point too
- I left the first loop constantly running to start over indefinitely
This code should be fine for your target:
import random
Olist = []
while True:
counter = 0
counter2 = 0
print("nWelcome to exercise 05")
InH1 = str(input("nDo you wish to continue? (y/n):"))
if InH1 == 'y':
list1 = random.sample(range(30), 11)
list2 = random.sample(range(30), 9)
print("n<><>Two random lists have been generated.<><>")
print(list1)
print(list2)
max_index = len(list2)-1
print("n<><>Max index has been generated.<><>")
print(max_index)
InD = str(input("nStart the func? (y/n):"))
if InD == 'n':
break
if InD == 'y':
print(list1[counter2])
print(list2[counter])
while counter < max_index:
if counter2 == 11:
print(Olist)
InL = str(input("nEnter any key to return to the start of the program,
or,n enter 'q' to break the program."))
if InL == 'q':
break
if list1[counter2] == list2[counter]:
Olist.append(list2[counter])
counter += 1
else:
counter += 1
if InH1 == 'n':
break
I’ve been doing some basic practice exercises for python, and so far it has been quite smooth flowing and such though I’ve run into a problem where the second while loop will not run. So far I’ve re-indented, defined the whole second and tried putting parameters on the loop where done = True, while done == True:, done = False
(later on in the code was what I previously had down but I removed it in effort to get it working.)
P.S some advice on my code would be greatly appreciated if you feel it necessary, as tbh I’m just winging it as I’ve been teaching myself how to code in python. I’ve included the first while loop too, as I figured that far down the code may be cause for the problem unknowingly.
import random
Olist = []
while True:
counter = 0
counter2 = 0
print("nWelcome to exercise 05")
InH1 = str(input("nDo you wish to continue? (y/n):"))
if InH1 == 'y':
list1 = random.sample(range(30), 11)
list2 = random.sample(range(30), 9)
print("n<><>Two random lists have been generated.<><>")
print(list1)
print(list2)
max_index = len(list2)-1
print("n<><>Max index has been generated.<><>")
InD = str(input("nStart the func? (y/n):"))
if InD == 'n':
break
if InD == 'y':
while True:
if counter2 == 11:
print(Olist)
InL = str(input("nEnter any key to return to the start of the program,
or,n enter 'q' to break the program."))
if InL == 'q':
break
if list1[counter2] == list2[counter]:
Olist.append(list2[counter])
counter + 1
elif counter == 10:
counter = 0
else:
counter + 1
if InH1 == 'n':
break
You wrote:
while True:
counter = 0
counter2 = 0
counter = 0
is OUTSIDE of the while-loop
Did you want the following?
while True:
counter = 0
counter2 = 0
if list1[counter2] == list2[counter]:
Olist.append(list2[counter])
counter + 1
elif counter == 10:
counter = 0
else:
counter + 1
I think your counter + 1
statements are causing you serious trouble. Really take a close look there… Don’t you mean counter = counter + 1
?
A few details:
- there is no action on
counter2
after you assigncounter2 = 0
so you will never enter theif counter2 == 11
branch [unreachable] and – unless needed somewhere else – you can completely remove that part - as said by Tom you have to ask the counter increment as
counter = counter + 1
or with the short formcounter += 1
- you have to control also that
counter
doesn’t get over themax_index
or you will try to fetch an unexisting element in the array generating an"IndexError: list index out of range"
from theif list1[counter2] == list2[counter]
line beforecounter
can be reset by the followingelif
branch - you need to create a condition to stop the second loop or you will not come to an end of the program: this is why I modified it into
while counter < max_index:
: the same line will allow you to safely remove theelif
branch of the previous point too - I left the first loop constantly running to start over indefinitely
This code should be fine for your target:
import random
Olist = []
while True:
counter = 0
counter2 = 0
print("nWelcome to exercise 05")
InH1 = str(input("nDo you wish to continue? (y/n):"))
if InH1 == 'y':
list1 = random.sample(range(30), 11)
list2 = random.sample(range(30), 9)
print("n<><>Two random lists have been generated.<><>")
print(list1)
print(list2)
max_index = len(list2)-1
print("n<><>Max index has been generated.<><>")
print(max_index)
InD = str(input("nStart the func? (y/n):"))
if InD == 'n':
break
if InD == 'y':
print(list1[counter2])
print(list2[counter])
while counter < max_index:
if counter2 == 11:
print(Olist)
InL = str(input("nEnter any key to return to the start of the program,
or,n enter 'q' to break the program."))
if InL == 'q':
break
if list1[counter2] == list2[counter]:
Olist.append(list2[counter])
counter += 1
else:
counter += 1
if InH1 == 'n':
break