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
  Updating a matrix in a time interval inside a for loop vp1989 4 145 May-17-2020, 07:15 PM
Last Post: vp1989
  calling strftime does not display current time tonytech 2 157 May-05-2020, 04:53 AM
Last Post: tonytech
  Time SQL Statement & Display Total anelliaf 0 168 Feb-28-2020, 05:55 PM
Last Post: anelliaf
  Loop independent of excecution time of a script Forelli 8 446 Feb-02-2020, 10:49 PM
Last Post: snippsat
  Running multiple script at the same time LoganSulpizio 1 376 Dec-07-2019, 04:30 PM
Last Post: Clunk_Head
  Time execution of a "for loop" with zip different in 2 equivalent context sebastien 1 280 Oct-11-2019, 11:07 AM
Last Post: sebastien
  pytest loop through multiple tests? burvil 0 762 Sep-26-2019, 11:42 PM
Last Post: burvil
  export file and display cmd prompt same time jacklee26 1 383 Jul-24-2019, 05:15 AM
Last Post: Larz60+
  output list reducing each time through loop 3Pinter 6 674 Mar-19-2019, 01:31 PM
Last Post: perfringo
  Pasting multiple images on to one image with a for loop. DreamingInsanity 2 752 Feb-01-2019, 12:39 PM
Last Post: DreamingInsanity

Forum Jump:


Users browsing this thread: 1 Guest(s)