Thread: Python Newbie

Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    36
    Rep Power
    11

    Python Newbie


    Hi I just started learning Python by reading a book.

    Can someone help me with this?

    I have to do the following print

    *
    **
    ***
    ****
    *****
    ******
    *******
    ********

    Here is my code
    Code:
    a = 0
    b = 0
    while a<8:
        print "*"
        while b <=a:
            print "*",
            b = b+1
        a=a+1
        b=0
    and here is the result

    *
    **
    ***
    ****
    *****
    ******
    *******
    ********
    ********

    It show the last line twice! I don't know why. Could someone help me with the basic question?

    Also can you give me pointers and tips with Python?

    I'm using IDLE to code and was wondering how can I like debug the code?

    Also, what is the equivalent of cin or getch() in Python?

    Thank you very much!

    If you can, can you give a simpler code to get to the result? I am trying to figure out how much simpler i can go with Python. It looks like my code is not the simplest I can do.
  2. #2
  3. slightly insane code guru
    Devshed Novice (500 - 999 posts)

    Join Date
    Jul 2004
    Location
    Indianapolis, IN
    Posts
    871
    Rep Power
    18
    first of all, when you post code put it inside CODE tags, so that indentation is preserved (a must with Python)

    this should work:
    Code:
    a = 1
    b = 1
    while a <= 8:
        while b <= a:
            print "*"
            b = b + 1
        a = a + 1
        b = 1
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    36
    Rep Power
    11
    Originally Posted by deltacoder
    first of all, when you post code put it inside CODE tags, so that indentation is preserved (a must with Python)

    this should work:
    Code:
    a = 1
    b = 1
    while a <= 8:
        while b <= a:
            print "*"
            b = b + 1
        a = a + 1
        b = 1
    Thanks, but the print ain't good. its shows this :

    *
    *
    *
    *
    etc.

    Thanks for the tip, I really new at this.

    Do you know the equivalent of cin or getchar() in Python?
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Location
    Bombay, India
    Posts
    159
    Rep Power
    13
    Let me try.....I'm skeptical if this helps but still....

    Code:
    >>> for i in range(1,9):
    ... 	print '*' * i
    ... 
    *
    **
    ***
    ****
    *****
    ******
    *******
    ********
    Rgds,
    Subha
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    35
    Rep Power
    11
    Code:
    a=8
    b=1
    c="*"
    while b <= 8:
    	print c*b
    	b += 1
    
    raw_input()
    hey, nokio where the hhell diid you get the idea to do it in such a complicated way???
  10. #6
  11. slightly insane code guru
    Devshed Novice (500 - 999 posts)

    Join Date
    Jul 2004
    Location
    Indianapolis, IN
    Posts
    871
    Rep Power
    18
    Originally Posted by nokio
    Thanks, but the print ain't good. its shows this :

    *
    *
    *
    *
    etc.

    Thanks for the tip, I really new at this.

    Do you know the equivalent of cin or getchar() in Python?
    whoops, forgot about the auto newlines :/ other examples already posted should work
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    36
    Rep Power
    11
    Originally Posted by ivanhope
    Code:
    a=8
    b=1
    c="*"
    while b <= 8:
    	print c*b
    	b += 1
    
    raw_input()
    hey, nokio where the hhell diid you get the idea to do it in such a complicated way???
    hey, thx all!

    I'm really new in this python language. I'm reading a book about it ... and haven't seen the for .... nor string or char .......

    only if .. while ....

    only at page 40 on 292 ...

    a long way to go until i get to your skills!

    Thanks again!

    There's so much too know with this language, it is so different from C/C++ ..... like the for .... I had no idea how to use it.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Location
    Bombay, India
    Posts
    159
    Rep Power
    13
    Nokio,
    You wld very well be a geek....don't have to go a looong way for that.

    At this point, I can think of this quote

    Never be afraid to try something new. Remember, amateurs built the ark. Professionals built the Titanic.

    Rgds,
    Subha
  16. #9
  17. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Originally Posted by nokio
    Do you know the equivalent of cin or getchar() in Python?
    Hey nikio,

    You're looking for the input() and raw_input() functions; though as a rule you should usually use raw_input() for safty . Anyway here's a simple example from my Python shell:

    Code:
    >>> input('Enter something! ')
    Enter something! "Hello"
    'Hello'
    >>> raw_input('Enter something! ')
    Enter something! Hello
    'Hello'
    >>> input('Enter something! ')
    Enter something! 19
    19
    >>> raw_input('Enter something! ')
    Enter something! 19
    '19'
    >>> input('Enter something! ')
    Enter something! 13 + 21
    34
    >>> raw_input('Enter something! ')
    Enter something! 13 + 21
    '13 + 21'
    >>>
    As you can see, raw_input() always returns a string where as input() evaluates an expression and returns the result.

    Hope this helps

    Mark.

    Welcome to the Python community! I'm sure you'll really enjoy it .
    programming language development: www.netytan.com Hula

  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    36
    Rep Power
    11
    Originally Posted by netytan
    Hey nikio,

    You're looking for the input() and raw_input() functions; though as a rule you should usually use raw_input() for safty . Anyway here's a simple example from my Python shell:

    Code:
    >>> input('Enter something! ')
    Enter something! "Hello"
    'Hello'
    >>> raw_input('Enter something! ')
    Enter something! Hello
    'Hello'
    >>> input('Enter something! ')
    Enter something! 19
    19
    >>> raw_input('Enter something! ')
    Enter something! 19
    '19'
    >>> input('Enter something! ')
    Enter something! 13 + 21
    34
    >>> raw_input('Enter something! ')
    Enter something! 13 + 21
    '13 + 21'
    >>>
    As you can see, raw_input() always returns a string where as input() evaluates an expression and returns the result.

    Hope this helps

    Mark.

    Welcome to the Python community! I'm sure you'll really enjoy it .
    Thanx alot Mark!

    That really helps alot.

    I was wondering if you or anyone could help me with this problem.

    I have to reverse a word .... let's say I enter a word (raw_input) and have to store it into another variable but reversed. How can i do that?

    Code:
    print "Entrer un mot : ",
    chaine1 = raw_input()
    chaine2 = ""
    length = len(chaine1)
    
    for i in range(0,length):
        chaine2 = chaine2 + chaine1[length-1-i]  //Which is illegal!
    Thanks in advance
  20. #11
  21. slightly insane code guru
    Devshed Novice (500 - 999 posts)

    Join Date
    Jul 2004
    Location
    Indianapolis, IN
    Posts
    871
    Rep Power
    18
    You might consider the fact that lists have a reverse() method... so if you convert the string to a list of characters...
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    Also, what is the equivalent of cin or getch() in Python?
    You can read a single keypress on Windows using the msvcrt.getch() function.

    There does not seem to be an equivalent for UNIX systems, perhaps because UNIX consoles are more stream-oriented. You could maybe do it with the curses module, but on a per-window basis.

    Dave - The Developers' Coach
  24. #13
  25. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    A reverse example using extended slices, which were added in Python 2.3. I've also included an example using the reverse() method mentioned by Delta.

    Code:
    >>> someInput = raw_input('Enter something! ')
    Enter something! raw_input
    >>>
    >>> someInput
    'raw_input'
    >>> someInput[::-1]
    'tupni_war'
    >>>
    >>> someInputList = list(someInput)
    >>> someInputList
    ['r', 'a', 'w', '_', 'i', 'n', 'p', 'u', 't']
    >>> someInputList.reverse()
    >>> someInputList
    ['t', 'u', 'p', 'n', 'i', '_', 'w', 'a', 'r']
    >>> ''.join(someInputList)
    'tupni_war'
    >>>
    Mark.
    programming language development: www.netytan.com Hula

  26. #14
  27. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    36
    Rep Power
    11
    whoa thanks guys, you are the best.

    But all those notions ... extended slice and list .... are too advanced for me yet even though i understand the concept of it.

    I was wondering how to reverse it with basic operations like for, while, if, etc ... because in the book i'm reading that how far i got. And i presume there must be a way to do it with basic operations.

    I hope I am not being too annoying ....

    Thanks,
  28. #15
  29. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Location
    Bombay, India
    Posts
    159
    Rep Power
    13
    Hi Nokio,
    You were right with your code...just a small error

    Code:
    >>> original=raw_input('Enter the string to be reversed........')
    >>> original
    'AIDNI'
    >>> reverse=''
    >>> 
    >>> for i in range(0,len(original)):
    ... 	reverse+=original[(len(original)-i)-1]
    ... 	
    >>> reverse
    'INDIA'
    >>>
    Thats what you want probably....but then using the reverse() function wld be a smarter way of doing it!

    Rgds,
    Subha
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo