Guillermo Vázquez
Guillermo Vázquez

Reputation: 159

How can I use a list in a for in range loop?

consonant_letters = ['B', 'C', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'V', 'X', 'Z']
for x in range(9999):
    for y in range(consonant_letters):
        for z in range(consonant_letters):
            for a in range(consonant_letters):
                print(f'{x} {y} {z} {a}')

*And what I get is this

TypeError: 'list' object cannot be interpreted as an integer

*I'm trying to print all the numbers in the Spanish License Plate system, transform it into a list and lastly, do an input function where the user enters a certain license plate, and the program tells him how many license plates there are until this license plate system ends.

*Edit: I found out now the only thing I have left and can't figure out is how to formulate the input function. Here's the solution:

consonant_letters = ['B', 'C', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'M',    
'N', 'P', 'Q', 'R', 'S', 'T', 'V', 'X', 'Z']

for x in range(10000):
    break
    if x < 1000 and x > 99:
        x = f"0{x}"
    elif x < 100 and x > 9:
        x = f"00{x}"
    elif x < 10:
        x = f"000{x}"
    for y in consonant_letters:
        for z in consonant_letters:
            for a in consonant_letters:
                print(f'{x} {y} {z} {a}')

*I tried introducing the input function so the user can introduce a certain license plate number and I will detect if it exists but there's a problem. Here's what I tried:

`consonant_letters = ['B', 'C', 'D', 'F', 'G', 'H', 'J', 'K', 'L',      
'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'V', 'X', 'Z']
for x in range(10000):
if x < 1000 and x > 99:
    x = f"0{x}"
elif x < 100 and x > 9:
    x = f"00{x}"
elif x < 10:
    x = f"000{x}"
for y in consonant_letters:
    for z in consonant_letters:
        for a in consonant_letters:
            list_1 = f'{x} {y}{z}{a}'
            license_plate_user = input("Write the license plate: ")
            print(license_plate_user in list_1)`

*When I introduce any other license plate that's not 0000 BBB (the first one) it says False. I know this means that the loop gets executed only one time, but I don't know how to fix it.

*Edit; I figured out how to get the loop executed and then formulate the input but I have one more question left. Is there an operation I can write knowing a specific license plate to know how many there are left?

consonant_letters = ['B', 'C', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'M',   
'N', 'P', 'Q', 'R', 'S', 'T', 'V', 'X', 'Z']
for x in range(10000):
if x < 1000 and x > 99:
    x = f"0{x}"
elif x < 100 and x > 9:
    x = f"00{x}"
elif x < 10:
    x = f"000{x}"
for y in consonant_letters:
    for z in consonant_letters:
        for a in consonant_letters:
            list1 = f'{x} {y}{z}{a}'
            if "9999 ZZZ" in list1:
                license_plate_user = input("Write the license plate: ")
                license_plate_user in list1
                if license_plate_user:
                    print("Operation I do not know yet")
                else:
                    print("Wrong values")

*Edit: I found a way but it's not working and I don't know why:

consonant_letters = ['B', 'C', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 
'N', 'P', 'R', 'S', 'T', 'V', 'X', 'Z']
total_number_of_license_plates = 80000000
for x in range(10000):
    if x < 1000 and x > 99:
        x = f"0{x}"
    elif x < 100 and x > 9:
        x = f"00{x}"
    elif x < 10:
        x = f"000{x}"
    for y in consonant_letters:
        for z in consonant_letters:
            for a in consonant_letters:
                list1 = f'{x} {y}{z}{a}'
                if "9999 ZZZ" in list1:
                    license_plate_user = input("Write the license   
plate: ")
                    license_plate_user in list1
                    if license_plate_user:
                        print(list1.index(license_plate_user))
                        license_plates_left = 
total_number_of_license_plates - list1.index(license_plate_user)
                        print(f'There are {license_plates_left} license 
plates left')
                    else:
                        print("Wrong values")

And what I get is:

ValueError: substring not found

Upvotes: 0

Views: 238

Answers (3)

Daweo
Daweo

Reputation: 36520

range accept integer or integers as inputs. In case you are not sure how to use given word you might launch python intrepeter and ask it - in this case:

help(range)

your code will work after replacing all range(consonant_letters) with consonant_letters but I want to note that your task is ideal place to use itertools.product. Consider following example:

import itertools
x = ['A','B','C']
for x1,x2,x3 in itertools.product(x,repeat=3):
    print(f'{x1} {x2} {x3}')

Output:

A A A
A A B
A A C
A B A
A B B
A B C
A C A
A C B
A C C
B A A
B A B
B A C
B B A
B B B
B B C
B C A
B C B
B C C
C A A
C A B
C A C
C B A
C B B
C B C
C C A
C C B
C C C

As you can see it allow you to use one for-loop instead of (in this case) 3 nested for-loops.

Upvotes: 1

chepner
chepner

Reputation: 531275

You can do this with one loop:

from itertools import product

for x, (y, z, a) in product(range(9999), product(consonant_letters, repeat=3)):
    print(f'{x} {y} {z} {a}')

Upvotes: 1

Amir
Amir

Reputation: 11

you can't use range() with list use this:

for x in range(9999):
    for y in consonant_letters:
        for z in consonant_letters:
            for a in consonant_letters:
                print(f'{x} {y} {z} {a}')

Upvotes: 1

Related Questions