Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
For loops index
#5
(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.
Reply


Messages In This Thread
For loops index - by rturus - Mar-23-2021, 08:48 AM
RE: For loops index - by Serafim - Mar-23-2021, 09:29 AM
RE: For loops index - by BashBedlam - Mar-23-2021, 02:51 PM
RE: For loops index - by deanhystad - Mar-23-2021, 03:27 PM
RE: For loops index - by rturus - Mar-23-2021, 03:28 PM
RE: For loops index - by deanhystad - Mar-23-2021, 03:34 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020