|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
||||
|
||||
|
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
|
|
#3
|
|||
|
|||
|
Quote:
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? |
|
#4
|
|||
|
|||
|
Let me try.....I'm skeptical if this helps but still....
Code:
>>> for i in range(1,9): ... print '*' * i ... * ** *** **** ***** ****** ******* ******** Rgds, Subha ![]() |
|
#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??? |
|
#6
|
||||
|
||||
|
Quote:
whoops, forgot about the auto newlines :/ other examples already posted should work ![]() |
|
#7
|
|||
|
|||
|
Quote:
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. |
|
#8
|
|||
|
|||
|
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 ![]() |
|
#9
|
||||
|
||||
|
Quote:
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 . |
|
#10
|
|||
|
|||
|
Quote:
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 |
|
#11
|
||||
|
||||
|
You might consider the fact that lists have a reverse() method... so if you convert the string to a list of characters...
![]() |
|
#12
|
|||
|
|||
|
Quote:
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 |
|
#13
|
||||
|
||||
|
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. |
|
#14
|
|||
|
|||
|
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, |