0

I'm running the following code from the command line (python filename.py) and it wont terminate. I've tried the code outside of a procedure and have tried the procedure in an online interpreter, so I don't think it's the the algorithm. What am I doing wrong?

    n = raw_input("Enter a number: ")

    def print_multiplication_table(n):

        x = 1

        while x <= n:
            y = 1
            while y <= n:
                z = x * y
                print x, " * ", y, " = ", z
                y += 1
            x += 1

    print_multiplication_table(n)
1
  • 2
    Please, fix your indentation. I have no idea what the code does/should do if I don't have the proper indentation. Commented Mar 4, 2012 at 17:14

2 Answers 2

4

You should convert the number received from raw_input into an integer. Right now it's being compared as a string.

An easy (but probably bad) way to do this:

n = int(raw_input("Enter a number: "))
Sign up to request clarification or add additional context in comments.

Comments

1

There is a problem with the raw_input command. I have a similar code myself (guess we're both following the Udacity course). I tried to add the raw_input line to my code, and it ended up in an infinite loop too.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.