[Tutor] python draw christman tree using loops

Joel Goldstick joel.goldstick at gmail.com
Sun Oct 19 15:43:26 CEST 2014


On Sun, Oct 19, 2014 at 9:27 AM, jarod_v6 at libero.it <jarod_v6 at libero.it> wrote:
> thanks for the help:
> In [60]: for i in range(10):
>    ....:   for t in range(6-i):
>    ....:       print " "
>    ....:       for item in range(i+1):
>    ....:           print "*"
>    ....:
>
> *
>
> *
>
> *
>
> *
>
> *
>
> *
>
> *
> *
>
> *
> *
>
> *
> *
>
> *
> *
>
> *
> *
>
> *
> *
> *
>
> *
> *
> *
>
> *
> *
> *
>
> *
> *
> *
>
> *
> *
> *
> *
>
> *
> *
> *
> *
>
> *
> *
> *
> *
>
> *
> *
> *
> *
> *
>
> *
> *
> *
> *
> *
>
> *
> *
> *
> *
> *
> *
>
> this i the output.. it is not a tree..
>
>
>>----Messaggio originale----
>>Da: joel.goldstick at gmail.com
>>Data: 19/10/2014 14.10
>>A: "jarod_v6 at libero.it"<jarod_v6 at libero.it>
>>Cc: <tutor-request at python.org>, "tutor at python.org"<tutor at python.org>
>>Ogg: Re: [Tutor] python draw christman tree using loops
>>
>>On Sun, Oct 19, 2014 at 7:32 AM, jarod_v6 at libero.it <jarod_v6 at libero.it>
> wrote:
>>>
>>> Dear All,
>>> or improve my understanding o python I would like to learn how to draw
> simple
>>> figures using loops.
>>> I start from this code in java:
>>> import java.util.Scanner;
>>>
>>> public class Eserc2_8 {
>>>     public static void main(String args[]) {
>>>         Scanner s = new Scanner(System.in);
>>>         // Altezza del triangolo
>>>         int altezza = s.nextInt();
>>>
>>>         for (int riga = 1; riga <= altezza; riga++) {
>>>             int i = 0;
>>>             while (i<altezza-riga) {
>>>                 System.out.print(" ");
>>>                 i += 1;
>>>             }
>>>             i = 0;
>>>             while (i< (riga*2)-1) {
>>>                 System.out.print("*");
>>>                 i += 1;
>>>             }
>>>             System.out.println("");
>>>         }
>>>     }
>>> }
>>>
>>> and I want to transfer to python loops:
>>>
>>> for i in range(10):
>>>     #space
>>>     for t in range(6-i):
>>>         print ""
>>>         for item in range(i+1):
>>>             print "."
>>>
>>>
>>> But I ha not succed. So someone could you please explain how can work'
>>> thanks  a lot
>>> bw
>>>
>>what is your output?  What errors (print complete traceback)
>>
>>You probably want print " ", and print ".",  so that there is no added
> newline.
>>>

In python 2.x you can add a comma to the print statement so that it
will not generate a newline.  That way your *s will print across.
After you loop use plain print to go to next line.

Also, reply to the list, not to me.
>>>
>>> _______________________________________________
>>> Tutor maillist  -  Tutor at python.org
>>> To unsubscribe or change subscription options:
>>> https://mail.python.org/mailman/listinfo/tutor
>>
>>
>>
>>--
>>Joel Goldstick
>>http://joelgoldstick.com
>>
>
>



-- 
Joel Goldstick
http://joelgoldstick.com


More information about the Tutor mailing list