little_angelx
Posts: 2
Joined: Sat Dec 27, 2014 8:59 am

multiplying

Sun Dec 28, 2014 8:48 am

Hi I have searched for the answer and couldn't find anything - I hope it's okay to ask. It's a really simple question (it doesn't bode well that I am stuck already!)

I am trying to write a really simple program to print out times tables. I actually got the code from a book too. However it seems to have gone a little crazy. Here is my code:

Code: Select all

tablenum=input("Which times tables would you like to display?")

for i in range(1,13):
       print (i, "x", tablenum, "=", tablenum*i)
This is what it prints out:

Which times tables would you like to display? 5
1 x 5 = 5
2 x 5 = 55
3 x 5 = 555
4 x 5 = 5555
.
.
.
and so on, right down to printing out 12 fives in a row.

What have I done???

Thanks for your help - sorry I am so thick!

User avatar
Laurens-wuyts
Posts: 716
Joined: Wed Aug 21, 2013 7:35 pm
Location: Belgium
Contact: Website

Re: multiplying

Sun Dec 28, 2014 8:54 am

You'll need to make the "tablenum" variable to an integer. Now it's a string. Add int() around your input like this:

Code: Select all

tablenum=int(input("Which times tables would you like to display?"))

for i in range(1,13):
       print (i, "x", tablenum, "=", tablenum*i)
Laurens
Last edited by Laurens-wuyts on Sun Dec 28, 2014 8:57 am, edited 1 time in total.

User avatar
PeterO
Posts: 5951
Joined: Sun Jul 22, 2012 4:14 pm

Re: multiplying

Sun Dec 28, 2014 8:56 am

Hint: Your tablenum variable is a string not a number....

"Hello" * 3 = "HelloHelloHello"

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

Return to “Beginners”