如何使用用户输入指定范围内所需的数字量

时间:2016-04-18 10:27:26

标签: python python-3.x input user-input

我需要用户输入来指定要在该范围内打印的数字量 我有以下代码:

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))))

2 个答案:

答案 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()