I got some programs that i need help with and it would be awesome if someone can help me out with them. I've been lurking around this site and hopefully someone can help me. Thank you


I m writing a program that should take number n and output it's sum of it.
for example if i enter 5 it should output 55
1^2+2^2+3^2+4^2+5^2=55
for far i m getting nothing.. a error of 15.. its adding up the numbers but not ^2 them.

def times(y):
    sum=0
    for i in range(1,y+1):

        sum+=i
    return sum
def main():
    x=input("Enter a number")
    w=times(x)
    print w
main()

Problem 2

this program is asking for numbers into words.. so far i got this

import string

def letters(l):
    a=str(l)
   
def main():
    x=raw_input("Enter some numbers to be written out")
    mylist=[]
    sentinel="*"
    while (x!=sentinel):
        y=eval(x)
        mylist.append(y)
        x=raw_input("Enter some numbers to be written out")       
    z=letters(mylist) 
    print z
main()

but in the program letters.. i was going to put in the if statements like this ..

if x==10:
            print "ten"
        if x==9:
            numnine=x
            print "nine"
        if x==8:
            numeight=x
            print "eight"
        if x==7:
            numseven=x
            print "seven"
        if x==6:
            numsix=x
            print "six"

but the thing is .. it has to go up to 999 i can write out all the numbers which will take me a whole day but there is got to be a shorter way of doing it

problem 3

i got to write this program in a recursively way.. where when i enter numbers in a list it should print out the reverse list in a exact way not using built in fuction.. i got it to work using reverse fuction but thats built in ..and i got it to print reverse but its sorting it..

here is the sorting way

import string
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")   
    w=reverse(mylist)
    print w
main()

well it was sorting but now its print none

lastly

how do i get started with a program to write the A-Z and returns its permutations ..
for example.. write ab.. it prints ab, ba, if i enter abc it print abc bca cab acb n etc

if anyone can help me with this.. .that would be awesome... thank you!!!

On problem #1 you want the sum of squares:

def times(y):
    sum = 0
    for i in range(1, y+1):
        # you want the sum of squares
        sum += i * i
    return sum

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

main()

This looks a lot like C, a bit more pythonic would be:

# sum of squares of a sequence 1 ... n
# if n is 5 then the sum is 1^2 + 2^2 + 3^3 + 4^4 + 5^5

n = 5

print sum([i*i for i in range(1, n+1)])  # 55

Man it's always little things that i always miss.. thanks .. i knew i had to do something with multiply i. thanks

I still need help with others.

Hey guys i figured out a way to do the problems.. i got little help from a friend.. i just need help with the problem number 2.. if someone can help me with that. .that would be awesome.! thanks again for all ur help people

Im very new at python, but as for the converting numbers to letters, im pretty sure you can make a list or whatever is the equivalent to an array in python, to solve the issue of having to type 0-999 out. would just be x = [0:1000] because it stops before the last number

Here's a start:

digits = {0:"zero, 1:"one", 2:"two", etc.}

print digits[2]

HTH,
Jeff

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.