我需要用户输入来指定要在该范围内打印的数字量 我有以下代码:
i = 0;
start_char = 65
end_char = 65
x = input("Enter numbers of plates:")
while (i < int(x)):
if i%26 == 0 and i!= 0:
end_char = (65)
start_char += 1
print((chr(start_char))+(chr(end_char)))
end_char =end_char + 1
i = i + 1
for plate_code in (range(1000)):
print(str(plate_code) + ((chr(start_char))+(chr(end_char))))
答案 0 :(得分:1)
您仅在i
内部if
递增,最初从i = 0
递增,您的代码永远不会进入if
并陷入无限循环。
将i = i + 1
移到if
之外。
while (i < int(x)):
if i%26 == 0 and i!= 0:
end_char = (65)
start_char += 1
print((chr(start_char))+(chr(end_char)))
end_char =end_char + 1
i = i + 1 #move incrementation outside of if
for plate_code in (range(1000)):
print(str(plate_code) + ((chr(start_char))+(chr(end_char))))
答案 1 :(得分:0)
只是不要尝试重新发明for
循环:
def main():
start_char = end_char = 65
plate_count = int(input('Enter numbers of plates:'))
for i in range(plate_count):
if i and i % 26 == 0:
end_char = 65
start_char += 1
print(chr(start_char) + chr(end_char))
end_char += 1
for plate_code in range(1000):
print('{}{}{}'.format(plate_code, chr(start_char), chr(end_char)))
if __name__ == '__main__':
main()
另外利用标准库的强大功能:
from itertools import islice, product
from string import ascii_uppercase
def main():
plate_count = int(input('Enter numbers of plates: '))
for start_char, end_char in islice(
product(ascii_uppercase, ascii_uppercase), plate_count
):
print('{}{}'.format(start_char, end_char))
for plate_code in range(1000):
print('{}{}{}'.format(plate_code, start_char, end_char))
if __name__ == '__main__':
main()