Python Forum

Full Version: For loops index
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
event = ["A","B","C","D"]


for x in range(4):
 
    maximum=(int(input("Enter the maximum points for the " + event[x]  + " event ")))
    if maximum<5 or maximum>50:
        
        x=x-1
        
        print("The score should be between 5 and 50")
        
        continue
    else:
        print("The maximum points for the " + event[x]  + " event is " ,maximum)
        print("\n----------------------------------------------")
       
        
If a user enter a number less than 5 or more than 50 the code should go back to loop but the index should not be incremented. I tried it but it seems x=x-1 has no effect on the increment of the index. Any idea or suggestion what I am doing wrong or what I can do differently?
Thanks.
Manually changing the index variable value in a for loop is not recommended. If you need a loop, set the limits outside of the loop and then enter the loop. If you have to change the index variable value inside the loop, then use a while loop and increment the index variable only in the case that you got a correct maximum value.
You are going to need two nested loops if you want it to stay on 'A' until a valid entry is made and then go on to 'B'.

event = ["A","B","C","D"]
 
for x in range(4):
	maximum = 0
	while maximum < 5 or maximum > 50 :
		maximum=(int(input("Enter the maximum points for the " + event[x]  + " event ")))
		if maximum<5 or maximum>50:
			print("The score should be between 5 and 50")
		else:
			print("The maximum points for the " + event[x]  + " event is " ,maximum)
			print("\n----------------------------------------------")
 
for x in range(4) does not use x, it generates x. This is what allows doing this:
for _ in range(4):
the recommended way to loop when the loop index is not used.

If you want to use the value of x to end the loop you should use while.
event = ["A","B","C","D"]
x = 0
while x < 4:
    maximum=(int(input(f"Enter the maximum points for the {event[x]} event ")))
    if 5 <= maximum <= 50:
        print(f"The maximum points for the {event[x]} event is {maximum}\n{'-'*45}")
        x += 1
    else:
        print("The score should be between 5 and 50")
(Mar-23-2021, 03:27 PM)deanhystad Wrote: [ -> ]for x in range(4) does not use x, it generates x. This is what allows doing this:
for _ in range(4):
the recommended way to loop when the loop index is not used.

If you want to use the value of x to end the loop you should use while.
event = ["A","B","C","D"]
x = 0
while x < 4:
    maximum=(int(input(f"Enter the maximum points for the {event[x]} event ")))
    if 5 <= maximum <= 50:
        print(f"The maximum points for the {event[x]} event is {maximum}\n{'-'*45}")
        x += 1
    else:
        print("The score should be between 5 and 50")

Thanks this works.
One minor tweak. May as well make the code flexible. Now you can add or remove events without having to search for "magic numbers" associated with the event list.
event = ["A","B","C","D"]
x = 0
while x < len(event):
    maximum=(int(input(f"Enter the maximum points for the {event[x]} event ")))
    if 5 <= maximum <= 50:
        print(f"The maximum points for the {event[x]} event is {maximum}\n{'-'*45}")
        x += 1
    else:
        print("The score should be between 5 and 50")