goto, cls, wait commands

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • BOOGIEMAN

    goto, cls, wait commands

    I've just finished reading Python turtorial for non-programmers
    and I haven't found there anything about some usefull commands I used in
    QBasic. First of all, what's Python command equivalent to QBasic's "goto" ?
    Secondly, how do I clear screen (cls) from text and other content ?
    And last, how do I put program to wait certain amount of seconds ?
    If I remeber correctly I used to type "Wait 10" and QBasic waits
    10 seconds before proceeding to next command.
  • Fouff

    #2
    Re: goto, cls, wait commands

    BOOGIEMAN a écrit :[color=blue]
    > I've just finished reading Python turtorial for non-programmers
    > and I haven't found there anything about some usefull commands I used in
    > QBasic. First of all, what's Python command equivalent to QBasic's "goto" ?[/color]
    I had a professor that told me that using goto in prog is that there is
    a mistake in the algorythm.
    If I remember, I think there is no goto instruction in python !
    [color=blue]
    > Secondly, how do I clear screen (cls) from text and other content ?[/color]
    I don't understand well what you exactly want to do. Can you explain
    more please.
    [color=blue]
    > And last, how do I put program to wait certain amount of seconds ?
    > If I remeber correctly I used to type "Wait 10" and QBasic waits
    > 10 seconds before proceeding to next command.[/color]
    import time
    time.sleep(10)

    Comment

    • Duncan Booth

      #3
      Re: goto, cls, wait commands

      BOOGIEMAN wrote:
      [color=blue]
      > I've just finished reading Python turtorial for non-programmers
      > and I haven't found there anything about some usefull commands I used
      > in QBasic. First of all, what's Python command equivalent to QBasic's
      > "goto" ?[/color]

      There isn't one. Why do you think you need this?
      [color=blue]
      > Secondly, how do I clear screen (cls) from text and other
      > content ?[/color]

      That depends on your computer, and how you are running your program.
      One way which *might* work is:

      import os
      os.system("cls" )
      [color=blue]
      > And last, how do I put program to wait certain amount of
      > seconds ? If I remeber correctly I used to type "Wait 10" and QBasic
      > waits 10 seconds before proceeding to next command.[/color]

      import time
      time.sleep(10)

      Comment

      • Christos TZOTZIOY Georgiou

        #4
        Re: goto, cls, wait commands

        On Thu, 10 Feb 2005 16:59:04 +0100, rumours say that BOOGIEMAN
        <BOOGIEMANPN@YA HOO.COM> might have written:

        Best advice: try to forget QBasic, and try again reading the tutorial. That, if
        your post is serious. If it isn't, keep reading my reply :)
        [color=blue]
        >I've just finished reading Python turtorial for non-programmers
        >and I haven't found there anything about some usefull commands I used in
        >QBasic. First of all, what's Python command equivalent to QBasic's "goto" ?[/color]

        goto for python:



        Please ignore the line in bold red.
        [color=blue]
        >Secondly, how do I clear screen (cls) from text and other content ?[/color]

        Python clears after itself, so you don't need to. If you insist though,

        ..>> import os
        ..>> os.system('cls' )

        That on a windows "console".

        If on IDLE, try closing the window and reopening it.
        [color=blue]
        >And last, how do I put program to wait certain amount of seconds ?
        >If I remeber correctly I used to type "Wait 10" and QBasic waits
        >10 seconds before proceeding to next command.[/color]

        (A serious answer for a change) Waiting is time related. So import time and
        call the time.sleep function.

        Try entering help at a Python prompt.
        --
        TZOTZIOY, I speak England very best.
        "Be strict when sending and tolerant when receiving." (from RFC1958)
        I really should keep that in mind when talking with people, actually...

        Comment

        • Grant Edwards

          #5
          Re: goto, cls, wait commands

          On 2005-02-10, BOOGIEMAN <BOOGIEMANPN@YA HOO.COM> wrote:
          [color=blue]
          > First of all, what's Python command equivalent to QBasic's "goto" ?[/color]

          There isn't one.

          One defines functions and calls them. One uses for and while
          loops. One uses list comprehensions. One uses if/elif/else.
          [color=blue]
          > Secondly, how do I clear screen (cls) from text and other content ?[/color]

          That depends on the host system. Under Unix, you can do
          os.system('clea r'). Or you can use ncurses. Or you can use
          os.system to run the 'tput' command with appropriate parameters
          -- see the tput man page.

          There's probably some way to do it in Windows as well, but I
          don't do windows.
          [color=blue]
          > And last, how do I put program to wait certain amount of
          > seconds ?[/color]

          time.sleep(1) will wait for 1 second.
          time.sleep(5.5) will wait for 5.5 seconds.

          --
          Grant Edwards grante Yow! Yow! I like my new
          at DENTIST...
          visi.com

          Comment

          • Alan Kennedy

            #6
            Re: goto, cls, wait commands

            [BOOGIEMAN][color=blue]
            > I've just finished reading Python turtorial for non-programmers
            > and I haven't found there anything about some usefull commands I used in
            > QBasic. First of all, what's Python command equivalent to QBasic's "goto" ?[/color]

            Oh no! You said the "G" word! That's a dirty word in computer science
            circles, because of the perception that "goto" (there, I said it, ugh!)
            can lead people to structure their code badly, i.e. write bad programs.

            Instead, most modern programming languages offer a range of control and
            looping constructs that allow you to code your intention more clearly
            than with goto. Python examples include "while", "for .. in ..", "try ..
            except ..", etc, etc.

            So in order to answer your question, you're probably going to have to be
            more specific on what you want "goto" for.

            Interestingly, "goto"s are undergoing a bit of renaissance in coding
            circles, but people have felt compelled to call them something
            different: continuations. But you're probably not interested in them.
            And python can't do them anyway.
            [color=blue]
            > Secondly, how do I clear screen (cls) from text and other content ?[/color]

            That depends on

            A: What type of display device you're using
            B: What type of interface is being rendered on that display (command
            line, GUI, IDE, etc)
            C: Perhaps what operating system you are using.
            [color=blue]
            > And last, how do I put program to wait certain amount of seconds ?
            > If I remeber correctly I used to type "Wait 10" and QBasic waits
            > 10 seconds before proceeding to next command.[/color]

            Ahhhhhh, a simple question! :-)

            import time
            time.sleep(10.0 )

            HTH,

            --
            alan kennedy
            ------------------------------------------------------
            email alan: http://xhaus.com/contact/alan

            Comment

            • Bruno Desthuilliers

              #7
              Re: goto, cls, wait commands

              Duncan Booth a écrit :[color=blue]
              > BOOGIEMAN wrote:
              >[/color]
              (snip)[color=blue][color=green]
              >>Secondly, how do I clear screen (cls) from text and other
              >>content ?[/color]
              >
              > That depends on your computer, and how you are running your program.
              > One way which *might* work is:
              >
              > import os
              > os.system("cls" )[/color]

              *might* work... !-)
              bruno@bibi modulix $ cls
              -bash: cls: command not found

              Bad luck ! didn't work !-)

              Bruno

              Comment

              • Bruno Desthuilliers

                #8
                Re: goto, cls, wait commands

                Grant Edwards a écrit :[color=blue]
                > On 2005-02-10, BOOGIEMAN <BOOGIEMANPN@YA HOO.COM> wrote:
                >
                >[color=green]
                >>First of all, what's Python command equivalent to QBasic's "goto" ?[/color]
                >
                >
                > There isn't one.
                >
                > One defines functions and calls them. One uses for and while
                > loops. One uses list comprehensions. One uses if/elif/else.[/color]

                and even sometimes break, continue, and return !-)

                Comment

                • Grant Edwards

                  #9
                  Re: goto, cls, wait commands

                  On 2005-02-10, Bruno Desthuilliers <bdesth.quelque chose@free.quel quepart.fr> wrote:[color=blue]
                  > Grant Edwards a écrit :[color=green]
                  >> On 2005-02-10, BOOGIEMAN <BOOGIEMANPN@YA HOO.COM> wrote:
                  >>
                  >>[color=darkred]
                  >>>First of all, what's Python command equivalent to QBasic's "goto" ?[/color]
                  >>
                  >>
                  >> There isn't one.
                  >>
                  >> One defines functions and calls them. One uses for and while
                  >> loops. One uses list comprehensions. One uses if/elif/else.[/color]
                  >
                  > and even sometimes break, continue, and return !-)[/color]

                  I forgot to mention try/except. When I do use goto in C
                  programming it's almost always to impliment what would have
                  been a try/except block in Python.

                  --
                  Grant Edwards grante Yow! ... the MYSTERIANS
                  at are in here with my
                  visi.com CORDUROY SOAP DISH!!

                  Comment

                  • Ulf Göransson

                    #10
                    Re: goto, cls, wait commands

                    Bruno Desthuilliers wrote:[color=blue]
                    > Duncan Booth a écrit :[color=green]
                    >> BOOGIEMAN wrote:[color=darkred]
                    >>> Secondly, how do I clear screen (cls) from text and other
                    >>> content ?[/color]
                    >>
                    >> That depends on your computer, and how you are running your program.
                    >> One way which *might* work is:
                    >>
                    >> import os
                    >> os.system("cls" )[/color]
                    >
                    > *might* work... !-)
                    > bruno@bibi modulix $ cls
                    > -bash: cls: command not found
                    >
                    > Bad luck ! didn't work !-)[/color]

                    Works for me! But then again...

                    kairos:ug> cat cls
                    #! /usr/local/bin/python
                    print "\xc",

                    /ug 8-)

                    Comment

                    • Pekka Niiranen

                      #11
                      Re: goto, cls, wait commands

                      import os
                      if os.name == "nt":
                      os.system("cls" ) # Works in w2k
                      else:
                      os.system("clea r") # Works in cygwin's Bash

                      Ulf Göransson wrote:[color=blue]
                      > Bruno Desthuilliers wrote:
                      >[color=green]
                      >> Duncan Booth a écrit :
                      >>[color=darkred]
                      >>> BOOGIEMAN wrote:
                      >>>
                      >>>> Secondly, how do I clear screen (cls) from text and other
                      >>>> content ?
                      >>>
                      >>>
                      >>> That depends on your computer, and how you are running your program.
                      >>> One way which *might* work is:
                      >>>
                      >>> import os
                      >>> os.system("cls" )[/color]
                      >>
                      >>
                      >> *might* work... !-)
                      >> bruno@bibi modulix $ cls
                      >> -bash: cls: command not found
                      >>
                      >> Bad luck ! didn't work !-)[/color]
                      >
                      >
                      > Works for me! But then again...
                      >
                      > kairos:ug> cat cls
                      > #! /usr/local/bin/python
                      > print "\xc",
                      >
                      > /ug 8-)[/color]

                      Comment

                      • BOOGIEMAN

                        #12
                        Re: goto, cls, wait commands

                        OK, thanks all
                        Here's presentation of my advanced programming skills :)
                        ----------------------------------------
                        import os
                        import time

                        os.system("cls" )

                        number = 78
                        guess = 0

                        while guess != number:
                        guess = input("Guess number: ")

                        if guess > number:
                        print "Lower"
                        time.sleep(3)
                        os.system("cls" )

                        elif guess < number:
                        print "Higher"
                        time.sleep(3)
                        os.system("cls" )

                        print "That's the number !"
                        ---------------------------------------
                        BTW, I'm thinking to replace lines "time.sleep (3)"
                        with something like "Press any key to guess again"
                        How do I do that ?

                        Also I wanted to put at the end something like
                        "Do you want to guess again ?" and then "GOTO" start
                        of program, but since there is no such command in Python
                        what are my possible solutions ?

                        Comment

                        • Brian van den Broek

                          #13
                          Re: goto, cls, wait commands

                          BOOGIEMAN said unto the world upon 2005-02-10 16:06:[color=blue]
                          > OK, thanks all
                          > Here's presentation of my advanced programming skills :)
                          > ----------------------------------------
                          > import os
                          > import time
                          >
                          > os.system("cls" )
                          >
                          > number = 78
                          > guess = 0
                          >
                          > while guess != number:
                          > guess = input("Guess number: ")
                          >
                          > if guess > number:
                          > print "Lower"
                          > time.sleep(3)
                          > os.system("cls" )
                          >
                          > elif guess < number:
                          > print "Higher"
                          > time.sleep(3)
                          > os.system("cls" )
                          >
                          > print "That's the number !"
                          > ---------------------------------------
                          > BTW, I'm thinking to replace lines "time.sleep (3)"
                          > with something like "Press any key to guess again"
                          > How do I do that ?
                          >
                          > Also I wanted to put at the end something like
                          > "Do you want to guess again ?" and then "GOTO" start
                          > of program, but since there is no such command in Python
                          > what are my possible solutions ?
                          >[/color]

                          Hi,

                          I'm no expert and I owe much of whatever I know to the Python Tutor
                          list. I'd suggest you check it out
                          <http://mail.python.org/mailman/listinfo/tutor>.

                          As for your situation, I'd do something like this untested code:

                          guess = input("Guess number: ")
                          while guess != number:
                          print `Nope.'
                          guess = raw_input('Gues s again? (Enter q for quit)')
                          if guess.lower() == 'q': # .lower() ensures 'Q' will match
                          print `Quitter!'
                          break
                          if guess > number:
                          # stuff here
                          if guess < number:
                          # different stuff here

                          raw_input is safer than input as it prevents malicious code from
                          ruining your day.

                          A while loop is the usual way to repeat something until some condition
                          is met. Here it repeats until guess == number. Another common idiom is

                          while True:
                          # do some stuff
                          if some_condition: # some condition being True signals the
                          break # need to break out of the loop

                          I game to Python 10'ish years after I'd programmed some in BASIC and
                          then not again. It took me a while to grok goto-less coding, too :-)

                          HTH,

                          Brian vdB

                          Comment

                          • Harlin

                            #14
                            Re: goto, cls, wait commands

                            No goto needed. If this makes no sense (which it may not if all you've
                            been exposed to is BASIC) it wouldn't be a bad idea to Google why you
                            should never use a goto statement.

                            To do a clear screen you'll need to use the method that your command
                            shell uses. The shortcut to this is for Windows, 'cls' and Linux (and
                            other Unix derivatives) is 'clear'. To put this into your programs
                            you'll need to import the OS module. Then use the method, system() to
                            run the command:

                            import os
                            os.system('cls' )

                            or

                            os.system('clea r')

                            To do a 'wait' you can use the Time module.

                            import time

                            seconds = 10 #This is an integer value
                            time.sleep(seco nds)

                            Only on Unix systems can you use the system command 'sleep'. However,
                            in the name of portability it's better to use the Python modules
                            whenever possible (they'll work on any system that supports Python).

                            Hope it helps.

                            Harlin

                            Comment

                            • Michael Hoffman

                              #15
                              Re: goto, cls, wait commands

                              BOOGIEMAN wrote:[color=blue]
                              > First of all, what's Python command equivalent to QBasic's "goto" ?[/color]

                              You can only use the goto function if you use Python with line numbers,
                              thusly:

                              """
                              10 import sys
                              20 real_stdout = sys.stdout
                              30 class fake_stdout(obj ect): pass
                              40 fake_stdout.wri te = lambda x, y: None
                              50 sys.stdout = fake_stdout()
                              60 import this
                              70 sys.stdout = real_stdout
                              80 d = {}
                              90 c = 65
                              100 i = 0
                              110 d[chr(i+c)] = chr((i+13) % 26 + c)
                              120 if i == 26: goto(150)
                              130 i += 1
                              140 goto(110)
                              150 if c == 97: goto(180)
                              160 c = 97
                              170 goto(100)
                              180 print "How zen it is:"
                              190 print "".join([d.get(c, c) for c in this.s])
                              """

                              z = dict((int(x[0]), " ".join(x[1:])) for x in (y.split() for y in (__doc__ or _).strip().spli tlines())); k = [0] + sorted(z.keys() ); m = dict((b,a) for a,b in enumerate(k)); l = k[1]

                              def goto(n): global l; l = k[m[n]-1]

                              while l and l <= k[-1]: exec z[l]; l = l != k[-1] and k[m[l]+1]

                              --
                              Michael Hoffman

                              Comment

                              Working...