astronautlevel
astronautlevel

Reputation: 488

How to use inputs in loops

I created a code that shows the digits in the Fibonacci sequence. I want a way to allow the user to enter the number of numbers shown. This is my code:

total = 1
total2 = 0
for i in range (*Number of numbers/2*):
    total = total + total2
    print (total)
    total2 = total + total2
    print (total2)
#Shows golden ratio
total3 = total2/total
print (total3)

Can someone help me? It would be great!

Upvotes: 0

Views: 444

Answers (2)

Ashwini Chaudhary
Ashwini Chaudhary

Reputation: 250881

python 2.x:

for i in xrange (int(raw_input())//2):    #use xrange() in python 2.x, 
                                          #it is similar to python 3.x's range()

python 3.x:

for i in range (int(input())//2):

Upvotes: 2

C0deH4cker
C0deH4cker

Reputation: 4057

Here is an example in Python 2.x of how to get an integer:

myNum = int(raw_input("Enter a number: "))
print(myNum + 1)

Edit: Untested Python 3 version:

myNum = int(input("Enter a number: "))
print(myNum + 1)

Upvotes: 2

Related Questions