4 Replies - 988 Views - Last Post: 08 July 2012 - 04:10 PM Rate Topic: -----

#1 crowdedchef  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 28-June 12

python program trying to print sequence of numbers

Posted 28 June 2012 - 07:14 PM

Hi, I'm trying to figure this problem.

This program is to get a list (a sequence) of numbers using a given equation, but once it's calculated, the intial two numbers aren't listed...



import math

def main():
    print ("This program prints a sequence of integer numbers using given square root equation."

    x, y = eval(input("Please enter the first two integers in sequence:"))
    z = math.sqrt (x**2 + y**2)

    n=int(input("Input the length of the sequence:"))
    for i in range(n):
        x, y = y, math.sqrt (x**2 + y**2)
        print ((y))
        
main()




Is This A Good Question/Topic? 0
  • +

Replies To: python program trying to print sequence of numbers

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2134
  • View blog
  • Posts: 3,270
  • Joined: 21-June 11

Re: python program trying to print sequence of numbers

Posted 29 June 2012 - 12:24 AM

That's because you're not printing them. If you want the first two numbers to show up, print them before the loop.
Was This Post Helpful? 2
  • +
  • -

#3 crowdedchef  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 28-June 12

Re: python program trying to print sequence of numbers

Posted 29 June 2012 - 12:28 AM

View Postcrowdedchef, on 28 June 2012 - 07:14 PM, said:

Hi, I'm trying to figure this problem.

This program is to get a list (a sequence) of numbers using a given equation, but once it's calculated, the intial two numbers aren't listed...



import math

def main():
    print ("This program prints a sequence of integer numbers using given square root equation."

    x, y = eval(input("Please enter the first two integers in sequence:"))
    z = math.sqrt (x**2 + y**2)

    n=int(input("Input the length of the sequence:"))
    for i in range(n):
        x, y = y, math.sqrt (x**2 + y**2)
        print ((y))
        
main()




I worked out a couple bugs but I'm having trouble implementing a couple suggestions... any help will be much appreciated.

import math

def main():
    print ("This program prints a sequence of integer numbers using given square root equation.")
    print ("Please enter the first two integers in sequence:")
 
    x, y = eval(input())
    n=eval(input("Input the length of the sequence:"))
    print ("The Integer Sequence is:")
    
    for i in range(n):
        z = math.sqrt (x**2 + y**2)
        print (round(z))
        x=y
        y=z
        
                     
main()


The suggestions are:
-need to loop n-2 times and have the first two integers are printed before the for loop
-need to store the rounded values of variables a and b using multiple assignment instead of storing the non-integer value

again, any help will be much appreciated...!!! Thank you in advance
Was This Post Helpful? 0
  • +
  • -

#4 BloodyInari  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 106
  • Joined: 16-November 09

Re: python program trying to print sequence of numbers

Posted 02 July 2012 - 04:57 PM

View Postsepp2k, on 29 June 2012 - 02:24 AM, said:

That's because you're not printing them. If you want the first two numbers to show up, print them before the loop.

as sepp2k put it, you still need to print these initial values,

maybe this?
#import math

def main():
    # x,y = input; basically makes x = y = input
    #so do it like this
    x = input("gimme number: ")
    y = input("gimme number: ")
    n = input("gimme number: ")

    #prints initial values
    print x
    print y

    #your loop
    for i in range(n-2):
        z = ((x**2)+(y**2))**(1/2.0)
        print round(z)
        x=y
        y=z
main()


however, you can also do this for your variables if you want
x,y,n  = input("gimme number: "), input("gimme number: "), input("gimme number: ")

print round(x),"\n",round(y)


This post has been edited by BloodyInari: 02 July 2012 - 05:01 PM

Was This Post Helpful? 0
  • +
  • -

#5 crowdedchef  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 28-June 12

Re: python program trying to print sequence of numbers

Posted 08 July 2012 - 04:10 PM

Here is the solution to the problem posted by the instructor...

import math

def main():
    print("This program prints out a sequence of integer numbers.")
    print("Each integer is the square root of previous two rounded")
    print("to the closest whole number.")

    a, b = eval(input("Please enter the first two integers: "))
    L = eval(input("Please enter the length of the sequence: "))
    print("The output sequence is: ")
    print(a)
    print(B)/>
    for i in range(L-2):
        c = round(math.sqrt(a**2 + b**2))
        print(c)
        a = b
        b = c        
    
main()



I haven't posted my final code, but came close... Thank you BloodyInari!!! Thank you again!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1