Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
January 22nd, 2013, 05:19 AM
 Dried Monkey
Registered User

Join Date: Jan 2013
Posts: 1
Time spent in forums: 31 m 39 sec
Reputation Power: 0
What is wrong with my multiplication table code? (New to Python)

Hello. I've been learning Python from Udacity.com and was told to create multiplication table following this format:
Code:
```print_multiplication_table(2)
#1 x 1 = 1
#1 x 2 = 2
#2 x 1 = 2
#2 x 2 = 4```

This is my code:
Code:
```def print_multiplication_table(n):
x = 1
y = 1
sx = str(x)
sy = str(y)
equation = sx + " * " sy + " = " str(sx * sy)
while x <= n:
while y <= x:
print equation
Y += 1
print equation
x += 1
y = 1```

This returns an output of:
Code:
```1 * 1 = 1
1 * 1 = 1
1 * 1 = 1
1 * 1 = 1
1 * 1 = 1
1 * 1 = 1```

What is causing this? I have access to the answer to the problem, but I don't want it solved for me. I'd like to know what is causing this output so I can work around it and finish the question. Thanks in advance.

#2
January 22nd, 2013, 07:33 AM
 SuperOscar
Contributing User

Join Date: Jul 2007
Location: Joensuu, Finland
Posts: 420
Time spent in forums: 1 Week 18 h 38 m 49 sec
Reputation Power: 66
Quote:
 Originally Posted by Dried Monkey Code: ```def print_multiplication_table(n): x = 1 y = 1 sx = str(x) sy = str(y) equation = sx + " * " sy + " = " str(sx * sy) while x <= n: while y <= x: print equation Y += 1 print equation x += 1 y = 1``` What is causing this?

It doesn’t matter how many times you change the values of x and y, because when the value of the variable “equation” was set, x and y were both 1.

Anyway, if I may suggest some ideas:
• it is usually unnecessary to have separate variables for numeric values and their printable counterparts; just use print formatting (.format() or the older % method)
• it’s usually bad practice to create results and produce output in the same function; in most cases it’s more convenient to have one function produce results and another to print the output.
__________________
My armada: openSUSE 13.1 (home desktop, home laptop), Ubuntu 13.04 (work laptop), FreeBSD 9.2 (server), Mythbuntu 12.04 LTS (HTPC), Bodhi Linux 2.0 & Windows 7 Ultimate (test desktop), Android 4.1.2 (tablet)

#3
January 22nd, 2013, 11:11 AM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,170
Time spent in forums: 1 Month 3 Weeks 2 Days 10 h 27 m 9 sec
Reputation Power: 455
Case sensitive python distinguishes between y and Y . Could have been transcription typo.
__________________
[code]Code tags[/code] are essential for python code!

#4
January 22nd, 2013, 11:36 AM
 dwblas
Contributing User

Join Date: May 2009
Posts: 414
Time spent in forums: 5 Days 5 h 50 m 37 sec
Reputation Power: 32
See if this gives you any ideas on how to do it
Code:
```n=3

for x in range(1, n):
for y in range(1, n):
print x, y ```
Also, I would suggest that you print str(sx * sy) as strings and numbers define "multiply" differently
Code:
```    sx = str(x)
sy = str(y)
equation = sx + " * " sy + " = " str(sx * sy) ```

Last edited by dwblas : January 22nd, 2013 at 11:42 AM.

 Viewing: Dev Shed Forums > Programming Languages > Python Programming > What is wrong with my multiplication table code? (New to Python)