Home > front end >  Error while making a multiplication table in Python
Error while making a multiplication table in Python

Time:11-07

i am currently making a multiplication table in Python that inputs the number and range and prints a table according to that. Unfortunately, my code is not working and it just keeps on printing the number input. Any fixes? Here's my code:

    #Make a table printer

import math
import threading

i = 1
num = input("Please enter the number.")
range = input("Please enter the range.")

while i < int(range):
    print(int(i) * int(num))

CodePudding user response:

You need to add i = 1 at the last line of your code.

It should look like this

i = 1
num = input("Please enter the number.")
end = input("Please enter the range.")

while i < int(end):
    print(int(i) * int(num))
    i  = 1

Currently you’re not incrementing your counter so it just runs on forever. Your will loop will stay true forever. You need to keep increasing i until it no longer is less than the range number.

Also range is a python reserved keyword. You should avoid using it because it helps you perform a function of creating iterables.

I have updated my code to reflect this.

CodePudding user response:

Your loop is going into infinite loop, due to fact that your while condition will never turn False. To fix this, make sure to increment the value of i, your while loop should look something like below

while i< int(range):
  print(int(i) * int(num))
  i = i  1 

CodePudding user response:

Just try to apply any breakpoint in your loop. Otherwise it's creating an infinite loop without providing your desired output. And another thing, try to take input as integer method. You can use range = int(input()) And try to avoid naming your variable 'range' as it is one of he reserved word in python which returns a sequence of numbers, starting from 0 by default, and increments by 1 .

  • Related