# python program trying to print sequence of numbers

Page 1 of 1

## 4 Replies - 589 Views - Last Post: 08 July 2012 - 04:10 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=284247&amp;s=6b86d43e8f010973667dbc76067ef370&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 crowdedchef

• New D.I.C Head

Reputation: 0
• 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

• D.I.C Lover

Reputation: 1716
• Posts: 2,588
• 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

• New D.I.C Head

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

## Re: python program trying to print sequence of numbers

Posted 29 June 2012 - 12:28 AM

crowdedchef, 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

• D.I.C Head

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

## Re: python program trying to print sequence of numbers

Posted 02 July 2012 - 04:57 PM

sepp2k, 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

• New D.I.C Head

Reputation: 0
• 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

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }