Python Forum
How to Display Multiple Time Tables With While Loop - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: How to Display Multiple Time Tables With While Loop (/thread-22361.html)



How to Display Multiple Time Tables With While Loop - ZQ12 - Nov-10-2019

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]


RE: How to Display Multiple Time Tables With While Loop - ichabod801 - Nov-10-2019

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.


RE: How to Display Multiple Time Tables With While Loop - ZQ12 - Nov-10-2019

Thanks a lot! It works. Appreciate the help.