GIDForums  

Go Back   GIDForums > Computer Programming Forums > Python Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 05-Mar-2008, 22:47
fonz87 fonz87 is offline
New Member
 
Join Date: Jan 2008
Posts: 12
fonz87 has a little shameless behaviour in the past

Little help part 3


Just wondering anyone here can help me getting started or show me where i can read how to write this program

i have to write a program where if i put 5 it should output 55

for example, when u put 5 it will compute 1^2+2^2+3^2+4^2+5^2 and the output is 55

i got a code where its adding up all the 1 2 3 4 5 and im getting a 15
and i also got a code where it's a factorial and when i put 5 it's times all the numbers and outputs a number but i aint getting 55
  #2  
Old 06-Mar-2008, 21:02
crystalattice's Avatar
crystalattice crystalattice is offline
Flame War Instigator
 
Join Date: Apr 2004
Location: San Diego
Posts: 1,534
crystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nice

Re: Little help part 3


Why don't you post your code so we can see how you're approaching the problem? I have an idea of what to do but I want to make sure it's not the same thing you've already tried.
__________________
Common Sense v2.0-Striving to make the world a little bit smarter.
  #3  
Old 07-Mar-2008, 22:13
fonz87 fonz87 is offline
New Member
 
Join Date: Jan 2008
Posts: 12
fonz87 has a little shameless behaviour in the past

Re: Little help part 3


Python Code:
def main():
    x=input("Enter a number")
    sum=0
    for i in range(1,x+1):
        sum+=i
    print sum
main()

OK here is the code where it adds up the numbers ... but it doesn't add and times the numbers by the cude. like it does this 1 + 2 + 3+ 4+5 .. it gives me 15 but it doesn't do 1^2+2^2+3^2+4^2+5^2 = 55 .. i wrote a code but i guess i didn't save it and now I'm pissed off

it's gotta be written in parameters...

so i was trying this
Python Code:
def times(y):
    sum=0
    for i in range(1,y+1):
        sum+=i


def main():
    x=input("Enter a number")
    w=times(x)

    print w

main()

but it doesn't work, not sure why it's doing that but it pissing me off
Last edited by admin : 08-Mar-2008 at 01:04. Reason: Please insert your example Python codes between [PY] and [/PY] tags
  #4  
Old 07-Mar-2008, 22:16
fonz87 fonz87 is offline
New Member
 
Join Date: Jan 2008
Posts: 12
fonz87 has a little shameless behaviour in the past

Re: Little help part 3


Also if you know how to print list backwards without sorting the list .. that would be awesome.. this is what i have so far done where i got the list to print backwards but its sorting them out.. we did sort in class and i try to use the same code but it doesn't work. its sorts them but it doesn't print the way i want them to do. like for example if i put in 1 2 3 9 5 0 5 it should print 5 0 5 9 3 2 1 but instead its print 9 5 5 3 2 1 0

here is the code

Python Code:
import string
import math

def reverse(L):
    m=len(L)
    for i in range(m+2):
        for j in range(m-1):
            if L[j]<L[j+1]:
                L[j],L[j+1]=L[j+1],L[j]
                
    


def main():
    x=raw_input("Enter numbers to reverse it")
    mylist=[]
    sentinel="*"
    while (x!=sentinel):
        y=eval(x)
        mylist.append(y)
        x=raw_input("Enter numbers to reverse it")
        
    reverse(mylist)

    print mylist

main()
Last edited by admin : 08-Mar-2008 at 01:05. Reason: Please insert your example Python codes between [PY] and [/PY] tags
  #5  
Old 12-Mar-2008, 20:59
crystalattice's Avatar
crystalattice crystalattice is offline
Flame War Instigator
 
Join Date: Apr 2004
Location: San Diego
Posts: 1,534
crystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nice

Re: Little help part 3


Your first problem doesn't appear to have you squaring the values at any point. Isn't that a requirement? All you're doing is adding the numbers together in the range. You have to have a step where each number is squared before it is added to the sum.

For your second problem, Python has a method for reversing lists. Check out the tutorial:http://docs.python.org/tut/node7.html. You don't have to create your own method to do it, unlike C/C++, which is one of the benefits of Python.
__________________
Common Sense v2.0-Striving to make the world a little bit smarter.
 
 

Recent GIDBlogToyota - 2008 July Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
floating point decimal to ascii conversion crazypal C Programming Language 5 18-Apr-2007 04:59
Re: Beginning Python Tutorial (Part 8) crystalattice Python Forum 0 02-Nov-2005 09:11
Re: Things to Avoid in C/C++ -- void main(), Part 10 WaltP C Programming Language 0 18-Oct-2005 20:25
[Tutorial] Pointers in C (Part II) Stack Overflow C Programming Language 0 27-Apr-2005 17:36

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 04:12.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.