Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 16th, 2004, 08:51 PM
nokio nokio is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Posts: 36 nokio User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 19 m 32 sec
Reputation Power: 5
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.

Reply With Quote
  #2  
Old November 16th, 2004, 09:13 PM
deltacoder's Avatar
deltacoder deltacoder is offline
slightly insane code guru
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jul 2004
Location: Indianapolis, IN
Posts: 871 deltacoder User rank is Sergeant (500 - 2000 Reputation Level)deltacoder User rank is Sergeant (500 - 2000 Reputation Level)deltacoder User rank is Sergeant (500 - 2000 Reputation Level)deltacoder User rank is Sergeant (500 - 2000 Reputation Level)deltacoder User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 2 h 8 m 36 sec
Reputation Power: 12
Send a message via AIM to deltacoder Send a message via MSN to 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
__________________

deltacoder.com :: W3C Standards :: PHP.net :: Google

Reply With Quote
  #3  
Old November 16th, 2004, 09:21 PM
nokio nokio is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Posts: 36 nokio User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 19 m 32 sec
Reputation Power: 5
Quote:
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?

Reply With Quote
  #4  
Old November 16th, 2004, 11:19 PM
NewPythoner NewPythoner is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Location: Bombay, India
Posts: 159 NewPythoner User rank is Corporal (100 - 500 Reputation Level)NewPythoner User rank is Corporal (100 - 500 Reputation Level)NewPythoner User rank is Corporal (100 - 500 Reputation Level)NewPythoner User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 43 m 45 sec
Reputation Power: 7
Send a message via Yahoo to NewPythoner
Let me try.....I'm skeptical if this helps but still....

Code:
>>> for i in range(1,9):
... 	print '*' * i
... 
*
**
***
****
*****
******
*******
********


Rgds,
Subha

Reply With Quote
  #5  
Old November 17th, 2004, 12:09 AM
ivanhope ivanhope is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Posts: 35 ivanhope User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 59 m 4 sec
Reputation Power: 5
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???

Reply With Quote
  #6  
Old November 17th, 2004, 12:42 AM
deltacoder's Avatar
deltacoder deltacoder is offline
slightly insane code guru
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jul 2004
Location: Indianapolis, IN
Posts: 871 deltacoder User rank is Sergeant (500 - 2000 Reputation Level)deltacoder User rank is Sergeant (500 - 2000 Reputation Level)deltacoder User rank is Sergeant (500 - 2000 Reputation Level)deltacoder User rank is Sergeant (500 - 2000 Reputation Level)deltacoder User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 2 h 8 m 36 sec
Reputation Power: 12
Send a message via AIM to deltacoder Send a message via MSN to deltacoder
Quote:
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

Reply With Quote
  #7  
Old November 17th, 2004, 01:46 AM
nokio nokio is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Posts: 36 nokio User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 19 m 32 sec
Reputation Power: 5
Quote:
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.

Reply With Quote
  #8  
Old November 17th, 2004, 02:01 AM
NewPythoner NewPythoner is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Location: Bombay, India
Posts: 159 NewPythoner User rank is Corporal (100 - 500 Reputation Level)NewPythoner User rank is Corporal (100 - 500 Reputation Level)NewPythoner User rank is Corporal (100 - 500 Reputation Level)NewPythoner User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 43 m 45 sec
Reputation Power: 7
Send a message via Yahoo to NewPythoner
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

Reply With Quote
  #9  
Old November 17th, 2004, 05:13 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,536 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 10 m 32 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
Quote:
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


Reply With Quote
  #10  
Old November 18th, 2004, 04:14 PM
nokio nokio is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Posts: 36 nokio User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 19 m 32 sec
Reputation Power: 5
Quote:
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

Reply With Quote
  #11  
Old November 18th, 2004, 04:38 PM
deltacoder's Avatar
deltacoder deltacoder is offline
slightly insane code guru
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jul 2004
Location: Indianapolis, IN
Posts: 871 deltacoder User rank is Sergeant (500 - 2000 Reputation Level)deltacoder User rank is Sergeant (500 - 2000 Reputation Level)deltacoder User rank is Sergeant (500 - 2000 Reputation Level)deltacoder User rank is Sergeant (500 - 2000 Reputation Level)deltacoder User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 2 h 8 m 36 sec
Reputation Power: 12
Send a message via AIM to deltacoder Send a message via MSN to deltacoder
You might consider the fact that lists have a reverse() method... so if you convert the string to a list of characters...

Reply With Quote
  #12  
Old November 18th, 2004, 04:56 PM
DevCoach DevCoach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2004
Location: London, England
Posts: 1,243 DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 5 h 41 m 59 sec
Reputation Power: 265
Quote:
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

Reply With Quote
  #13  
Old November 18th, 2004, 06:36 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,536 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 10 m 32 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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.

Reply With Quote
  #14  
Old November 18th, 2004, 07:26 PM
nokio nokio is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Posts: 36 nokio User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 19 m 32 sec
Reputation Power: 5
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,

Reply With Quote