Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 How to Display Multiple Time Tables With While Loop
#1
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]
Quote
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote
#3
Thanks a lot! It works. Appreciate the help.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Time execution of a "for loop" with zip different in 2 equivalent context sebastien 1 120 Oct-11-2019, 11:07 AM
Last Post: sebastien
  pytest loop through multiple tests? burvil 0 221 Sep-26-2019, 11:42 PM
Last Post: burvil
  export file and display cmd prompt same time jacklee26 1 189 Jul-24-2019, 05:15 AM
Last Post: Larz60+
  output list reducing each time through loop 3Pinter 6 451 Mar-19-2019, 01:31 PM
Last Post: perfringo
  Pasting multiple images on to one image with a for loop. DreamingInsanity 2 336 Feb-01-2019, 12:39 PM
Last Post: DreamingInsanity
  Time multiple functions within functions Cortessizzz 4 567 Jan-09-2019, 04:15 PM
Last Post: Cortessizzz
  How to display <IPython.core.display.HTML object>? pythopen 2 11,890 Oct-17-2018, 06:22 PM
Last Post: pythopen
  Multi-processing - problem with running multiple *.py files at the same time Antonio 5 715 Sep-12-2018, 01:08 PM
Last Post: volcano63
  How to add multiple tables to pyspark sql cpatte7372 2 635 Jul-30-2018, 01:01 PM
Last Post: cpatte7372
  one time loop Skaperen 8 1,178 Jul-26-2018, 02:45 PM
Last Post: Skaperen

Forum Jump:


Users browsing this thread: 1 Guest(s)