1

I'm making this program that asks you for a number then prints out the first 1000 terms of that number's times table. I'm using Python 3x The output should be:

http://snag.gy/KxM37.jpg

But instead it gives me this: http://snag.gy/SQTAe.jpg

This is the code:

multiplication = 0
firstnumber = int(input("Enter a number: "))
number = firstnumber
for j in range(0, 1001):
 for i in range(0, 1001):
 multiplication = multiplication+1
 number = number*multiplication
 print(str(multiplication) + " times " + str(firstnumber) + " is " + str(number))

Thanks

Turbo
3193 gold badges6 silver badges16 bronze badges
asked Nov 2, 2013 at 19:59
10
  • why are there two loops? Commented Nov 2, 2013 at 20:01
  • developers.google.com/edu/python Commented Nov 2, 2013 at 20:02
  • @dm03514: If there shouldn't be, please improve it and answer my question. Thanks! Commented Nov 2, 2013 at 20:03
  • You’re doing the factorial of 1–1000 times 1001^1000. That’s kind of fun. Anyways, it looks like it should be firstnumber * multiplication, not number * multiplication. Commented Nov 2, 2013 at 20:03
  • Change the 1001 to a smaller number like 5 and you'll probably see what's wrong with your code. Commented Nov 2, 2013 at 20:03

3 Answers 3

2

I find it easier to think through the problem before trying to start coding.

You have the first step: Get a number from the user

I think the second step consists of going from 0 To 1000 and multiplying that number. In psuedo-code:

users_number = some_number
for num from 0 - 1000:
 print(num * usernumber)
answered Nov 2, 2013 at 20:03
Sign up to request clarification or add additional context in comments.

2 Comments

Your answer nor the other works. I might choose this one if you explain to me why this error happened: for num from 0 - 1000: ^ SyntaxError: invalid syntax
@Discovery, It is not valid python, it is fake code, since you are learning i left as an exercise to you to actually write the correct python!:)
1

Your problem is that you update number and keep multiplying it. You foresaw this problem and created a variable called firstnumber to tackle it, but you forgot to use it. Here’s what you meant to do:

>>> multiplication = 0
>>> firstnumber = int(input("Enter a number: "))
Enter a number: 17
>>> number = firstnumber
>>> number = firstnumber
>>> for j in range(0, 1001):
... for i in range(0, 1001):
... multiplication = multiplication+1
... number = firstnumber * multiplication
... print(str(multiplication) + " times " + str(firstnumber) + " is " + str(number))
... 
1 times 17 is 17
2 times 17 is 34
3 times 17 is 51
4 times 17 is 68
5 times 17 is 85
6 times 17 is 102
7 times 17 is 119
8 times 17 is 136
9 times 17 is 153
10 times 17 is 170
11 times 17 is 187
12 times 17 is 204
13 times 17 is 221
14 times 17 is 238
15 times 17 is 255
16 times 17 is 272

You are likely, however, much better off, to do something like this:

number = int(input("Enter a number: "))
mult = int(input("How many multiples: "))
for i in range(mult+1):
 print("%d times %d is %d" %(number, i, number*i))
Ry-
226k56 gold badges496 silver badges504 bronze badges
answered Nov 2, 2013 at 20:04

9 Comments

Why are there two loops?
@minitech: because that’s only very mildly related to the problem. I was trying to demonstrate the biger problems in OP’s code first. The next edit was going to contain an optimized version of what OP wants.
When I try what you told me, it won't work, it gives this: Enter a number: 1 1001 times 1 is 1001 2002 times 1 is 2002 3003 times 1 is 3003 4004 times 1 is 4004 5005 times 1 is 5005 6006 times 1 is 6006 7007 times 1 is 7007 What should I do?
I wanted 1 times by 1 is 1, 2 times by 1 is 2
@Discovery So, if this answer doesn't give you what you want for a result, is it better than dm03514's answer?
|
0

Might not be the best code, but better than what you were trying.

given_number = int(input("Enter a number: "))
for multiplier in range(1,1001):
 print("{0:4} times {1} is {2}".format(multiplier, given_number, multiplier*given_number))
answered Nov 2, 2013 at 20:58

2 Comments

I don't have enough reputation to vote up though I wanted much more simpler code. Thanks anyway
Well, I just added a bit of string formatting to prettify the output which you can see when you run my script. The numbers 0, 1, 2 represent the indices of the arguments passed to the format method. And, helping is priority. :)

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.