Closed Thread
Results 1 to 7 of 7

Thread: my first python

  1. #1
    kiddies is offline Programmer
    Join Date
    May 2009
    Posts
    129
    Rep Power
    0

    my first python

    Code:
    #!/usr/local/bin/python
    #fibonacci	
    
    	print "this fibonaci"
    	deret = raw_input("input your rows = ")
    	
    	a = 0
    	b = 1
    	
    	while a < deret :
    	
    	c = a + b 
    	print c,
    	
    	a = b
    	b = c
    	
    	a = a + 1


    hey this dont work properly, can anybody correct this c0de.....

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Jul 2006
    Posts
    16,486
    Blog Entries
    75
    Rep Power
    143

    Re: my first python

    python uses indentation to control loops. Your while loop doesn't contain anything.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  4. #3
    kiddies is offline Programmer
    Join Date
    May 2009
    Posts
    129
    Rep Power
    0

    Re: my first python

    while a < deret
    ....
    ....

    a = a+1

    that loop control, is wrong control

  5. #4
    Join Date
    Mar 2008
    Posts
    7,145
    Rep Power
    86

    Re: my first python

    Everything that should be in your while loop needs to be tabbed in once. Python uses whitespace to indicate loops. As is, your while loop is an infinite loop that does nothing.

  6. #5
    Hignar's Avatar
    Hignar is offline Programming Expert
    Join Date
    May 2009
    Posts
    419
    Blog Entries
    2
    Rep Power
    12

    Re: my first python

    There's a couple of problems with your code actually. The first and most obvious is that the loop needs to be indented. As it is python considers the loop to be empty and thus the code below never runs as nothing happens within the loop to change the value of a or deret. the code for your loop should look like this

    Code:
    while a < deret:
        c = a + b 
        print c,
    	
        a = b
        b = c
    	
        a = a + 1
    However, even with this sorted your loop will never end. The logic statement for the while loop is comparing a, an integer, to deret, a string. Python considers strings to be greater than integers so a < deret will always return true.

    you need to convert deret to an interger

    deret = raw_input("input your rows = ")
    deret = int(deret)
    As an aside, what is your code intended to do? Do you want to print a fibonacci sequence with terms less than the user input? You may want to check your logic if this is what you're tring to do.

  7. #6
    psam is offline Learning Programmer
    Join Date
    Jun 2009
    Posts
    34
    Rep Power
    10

    Re: my first python

    Instead of using the int(raw_input()) i believe you should use the input() function and i don't understand why you added a (,) after the print. Besides there's no need for the c variable.

    Your code should be more less like this :

    Code:
    a, b, choice = 0, 1, input('Some text here.')
    while a < choice:
        a, b = a + b, a
        if a < choice: print a
    Last edited by psam; 06-23-2009 at 04:48 AM.

  8. #7
    kiddies is offline Programmer
    Join Date
    May 2009
    Posts
    129
    Rep Power
    0

    Re: my first python

    ok thanks .....i will try n correct my script

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Advanced Python, part 1: Extending Python with C
    By spyder in forum Python Tutorials
    Replies: 0
    Last Post: 07-31-2010, 09:36 AM
  2. Replies: 11
    Last Post: 07-31-2010, 12:52 AM
  3. Power Of Python - What Has Been Written In Python?
    By Raja Sekharan in forum Python
    Replies: 7
    Last Post: 02-02-2009, 11:55 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts