While loop assistance

Hello! I’m having trouble formulating the right criterion for the while loop that I need to add here. This is the practice question prompt:

“Modify the program to include two-character .com names, where the second character can be a letter or a number, e.g., a2.com. Hint: Add a second while loop nested in the outer loop, but following the first inner loop, that iterates through the numbers 0-9.”

I understand that the loop doesn’t end, but I can’t sort out the right operator to a) initiate only after the alphabet is exhausted in the previous loop, and b) end appropriately. Any help is appreciated!

'''
Program to print all 2-letter domain names.
Note that ord() and chr() convert between text and ASCII/ Unicode encodings.
ord('a') is 97. ord('b') is 98, and so on. chr(99) is 'c', etc.
'''
print('Two-letter domain names:')

letter1 = 'a'
letter2 = '?'
while letter1 <= 'z':  # Outer loop
    letter2 = 'a'
    while letter2 <= 'z':  # Inner loop
        print('{}{}.com'.format(letter1, letter2))
        letter2 = chr(ord(letter2) + 1)
    while letter2 > 'z':  #This is the part I'm stuck on.
        for num in range(10):
            print('{}{}.com'.format(letter1, num))
    letter1 = chr(ord(letter1) + 1)

Thank you. I was trying to follow the instructions in the lesson, which called for adding another while loop, but I’m finding the lessons in my required textbook don’t always lead to the most efficient method. Your suggestion makes more sense. Much appreciated!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.