Python Forum
For loops index - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: For loops index (/thread-33021.html)



For loops index - rturus - Mar-23-2021

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.


RE: For loops index - Serafim - Mar-23-2021

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.


RE: For loops index - BashBedlam - Mar-23-2021

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----------------------------------------------")
 



RE: For loops index - deanhystad - Mar-23-2021

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")



RE: For loops index - rturus - Mar-23-2021

(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.


RE: For loops index - deanhystad - Mar-23-2021

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")