Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Setting For Loop Counter
#1
I am trying to set the counter to the one less if the input does not match with list. However, it seems that it does not set the counter. Any idea please?

team=["Arsenal","Liverpool","New Castle","Tottenham"]

t_ins=[]

count=0

for i in range(count,4):
    names=str(input("Please enter the name of a Football Club "))
    if names not in team:
        print("The name inputed does not exist. The team names are ",team)
        print("Try Again ")
        count=count-1
        continue
    else:
        t_ins.append(names)


print(t_ins)
Output:
Please enter the name of a Football Club chelse The name inputed does not exist. The team names are ['Arsenal', 'Liverpool', 'New Castle', 'Tottenham'] Try Again Please enter the name of a Football Club Arsanal The name inputed does not exist. The team names are ['Arsenal', 'Liverpool', 'New Castle', 'Tottenham'] Try Again Please enter the name of a Football Club Arsenal Please enter the name of a Football Club Liverpool ['Arsenal', 'Liverpool']
Reply
#2
I do not understand your question. Do you want to have a list with 4 valid team names?
If it's the case, the range won't help.

Maybe this is what you want:
# plural
teams = ["Arsenal", "Liverpool", "New Castle", "Tottenham", "Borussia Dortmund"]
t_ins = []

REQUIRED = 4

print("Teams:", ", ".join(teams))
# Run while loop until t_ins has the REQUIRED number of elements
while len(t_ins) != REQUIRED:
    # str() is not required, input returns a str
    # names is plural, but it's only one name
    name = input("Please enter the name of a Football Club: ")

    if name not in teams:
        print("The name inputed does not exist. The team names are:", ", ".join(team))
        print("Try Again")
        continue
    if name in t_ins:
        print("You have already selected this Football Club")
        continue
    else:
        t_ins.append(name)


print(t_ins)
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
A range is an immutable sequence. It cannot be changed. Use a while loop if you don't know how many times the loop needs to execute.

The way you are attempting to change range makes me think you don't understand how Python variables work either. A python variable is a lot like a key in a python dictionary. It is only a name that is used to reference the value, not the value itself.

In the code below I make my own range-like iterator. I use variables to reference the start, stop and step values. After I "initialize" the range-like iterator I try to mess with the stop and step, but the changes have no effect. Why is that? Is range-like also an immutable sequence?
def range_like(start, stop, step=1):
    index = start
    while index < stop:
        yield index
        index += step

start = 0
stop = 5
step = 1
for x in range_like(start, stop, step):
    stop = stop - 1
    step = step + 1
    print("x")
Output:
0 1 2 3 4
To test that hypothesis, I modify range-like to accept arguments as a list. While iterating I modify the values in the list and see if this changes how range-like operates.
def range_like(args):
    index = args[0]
    while index < args[1]:
        yield index
        index += args[2]

args = [0, 5, 1]
for x in range_like(args):
    args[1] -= 1
    args[2] += 1
    print(x)
Output:
0 2
This time changing the stop and step arguments changes the sequence.

The reason the two examples work differently is that in the first example I never changed the stop and step arguments. I reassign the stop and step variables, but range_like() does not use the stop and step variables. When I called range_like(start, stop, step) at the top of the for loop, I passed the objects referenced by these variables. Instead of stop, I passed 5. Instead of step I passed 1. Reassigning the variable names to reference different objects has no effect on the objects themselves.

In the second example I passed a list object. When I made changes to args inside the for loop I was actually modifying the list referenced by the "args" variable. Since the for loop and the range_like generator were both using the same list object, changes to the list changed the sequence produced by the generator.

To sum up:
Variables are names that reference objects. They are not the objects themself

Re-assigning a variable does not change the object previously referenced by the variable.

When you use a variable in a function argument list, you are not passing the variable to the function, you are passing the object referenced by the variable.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Setting maximum value of Range() in For loop pmt 4 2,597 Aug-04-2019, 02:38 PM
Last Post: pmt
Question [Help] How to end While Loop using counter? {Screenshot attached} vanicci 2 3,085 Aug-02-2018, 10:09 PM
Last Post: vanicci
  [Tkinter] Loop Counter for OptionMenu Creation JP_ROMANO 4 5,114 Sep-29-2017, 06:29 PM
Last Post: JP_ROMANO

Forum Jump:

User Panel Messages

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