Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 22nd, 2013, 04:19 AM
Dried Monkey Dried Monkey is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 1 Dried Monkey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #2  
Old January 22nd, 2013, 06:33 AM
SuperOscar SuperOscar is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2007
Location: Joensuu, Finland
Posts: 404 SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 5 h 6 m 41 sec
Reputation Power: 65
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 12.3 (home desktop, laptop, work desktop), Ubuntu 12.04 LTS (mini laptop), Debian GNU/Linux 7.0 (server), Mythbuntu 12.04 LTS (HTPC), Bodhi Linux 2.0 & Windows 7 Ultimate (test desktop), FreeBSD 9.1 (test server)

Reply With Quote
  #3  
Old January 22nd, 2013, 10:11 AM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,364 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 10 h 28 m 48 sec
Reputation Power: 383
Case sensitive python distinguishes between y and Y . Could have been transcription typo.
__________________
[code]Code tags[/code] are essential for python code!

Reply With Quote
  #4  
Old January 22nd, 2013, 10:36 AM
dwblas dwblas is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2009
Posts: 293 dwblas User rank is Corporal (100 - 500 Reputation Level)dwblas User rank is Corporal (100 - 500 Reputation Level)dwblas User rank is Corporal (100 - 500 Reputation Level)dwblas User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 3 Days 18 h 40 m 37 sec
Reputation Power: 7
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 10:42 AM.

Reply With Quote
Reply

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

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

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


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap