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}')
*我得到的是这个
TypeError: 'list' object cannot be interpreted as an integer
*我试图打印西班牙车牌系统中的所有数字,将其转换为列表,最后,执行输入功能,用户输入某个车牌,程序会告诉他在该车牌系统结束之前有多少个车牌
*编辑:我现在发现,我唯一没有弄清楚的是如何表述输入函数。以下是解决方案:
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}')
*我尝试引入输入功能,以便用户可以引入特定的车牌号,我将检测它是否存在,但存在问题。 以下是我尝试过的:
`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)`
*当我介绍任何其他不是0000 BBB(第一个)的车牌时,它会说False。我知道这意味着循环只执行一次,但我不知道如何修复它
*编辑;我知道如何执行循环,然后制定输入,但我还有一个问题要问。是否有一个操作,我可以写知道一个具体的车牌,知道有多少剩余
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")
*编辑:我找到了一种方法,但它不起作用,我不知道为什么:
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")
我得到的是:
ValueError: substring not found
range接受一个或多个整数作为输入。如果您不确定如何使用给定的单词,您可以启动python intrepeter并询问它-在本例中:
在用
consonant_letters
替换所有range(consonant_letters)
之后,您的代码将正常工作,但我想指出,您的任务是使用itertools.product
的理想场所。考虑下面的例子:输出:
如您所见,它允许您使用一个
for
循环,而不是(在本例中)3个嵌套的for
循环您可以使用一个循环执行此操作:
不能将range()与列表一起使用。请使用以下选项:
相关问题 更多 >
编程相关推荐