Python Forum

Full Version: How to Display Multiple Time Tables With While Loop
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm trying to write a code which prompts the user to enter a number, then prints its ten-times tables, before moving onto another number (with sentinel value). My code here is not working. The inner while loop is not executing the second time and instead it is simply taking an input, then asking for another input, and again, and again. Any ideas on how to fix this?:

num = int()
counter = int()
counter = 0
counter2 = int()
counter2 = 0


number = num

while counter2 != -1:
    num = int(input("\nEnter a Number: "))   
    counter2 += 1
    while counter <= 9 and num != 1:
        number = num * (counter + 1)
        print(number, end = " ")
        counter += 1
        number = num * (counter + 1)
[python][python]
[/python][/python]
You need to reset counter to 0 after the inner loop is done executing. Otherwise it retains the final value of 10, and the inner loop never executes.

Also, you don't need to declare variable types in Python, so you don't need lines like num = int(). It is sufficient to use num = 0.
Thanks a lot! It works. Appreciate the help.