goto, cls, wait commands

B

BOOGIEMAN

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.
 
F

Fouff

BOOGIEMAN a écrit :
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" ?
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 !
Secondly, how do I clear screen (cls) from text and other content ?
I don't understand well what you exactly want to do. Can you explain
more please.
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.
import time
time.sleep(10)
 
D

Duncan Booth

BOOGIEMAN said:
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" ?

There isn't one. Why do you think you need this?
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")
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.

import time
time.sleep(10)
 
C

Christos TZOTZIOY Georgiou

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 :)
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" ?

goto for python:

http://entrian.com/goto/index.html

Please ignore the line in bold red.
Secondly, how do I clear screen (cls) from text and other content ?

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.
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.

(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.
 
G

Grant Edwards

First of all, what's Python command equivalent to QBasic's "goto" ?

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.
Secondly, how do I clear screen (cls) from text and other content ?

That depends on the host system. Under Unix, you can do
os.system('clear'). 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.
And last, how do I put program to wait certain amount of
seconds ?

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

Alan Kennedy

[BOOGIEMAN]
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" ?

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.
Secondly, how do I clear screen (cls) from text and other content ?

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.
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.

Ahhhhhh, a simple question! :)

import time
time.sleep(10.0)

HTH,
 
B

Bruno Desthuilliers

Duncan Booth a écrit :
BOOGIEMAN wrote:
(snip)

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

import os
os.system("cls")

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

Bad luck ! didn't work !-)

Bruno
 
B

Bruno Desthuilliers

Grant Edwards a écrit :
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.

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

Grant Edwards

Grant Edwards a écrit :

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

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.
 
?

=?ISO-8859-1?Q?Ulf_G=F6ransson?=

Bruno said:
Duncan Booth a écrit :

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

Bad luck ! didn't work !-)

Works for me! But then again...

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

/ug :cool:
 
P

Pekka Niiranen

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

BOOGIEMAN

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 ?
 
B

Brian van den Broek

BOOGIEMAN said unto the world upon 2005-02-10 16:06:
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 ?

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('Guess 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
 
H

Harlin

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('clear')

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

import time

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

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
 
M

Michael Hoffman

BOOGIEMAN said:
First of all, what's Python command equivalent to QBasic's "goto" ?

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(object): pass
40 fake_stdout.write = 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().splitlines())); 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]
 
D

Dan Bishop

Harlin said:
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.

"GOTO" isn't even needed in QBasic (except for "ON ERROR GOTO").
 
J

jean-michel

BOOGIEMAN said:
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.

Hi all,
I saw a lot of comments saying GOTO is not usefull, very bad, and we
should'nt use it because we don't need it.
I think that's true, but only if you *create* programs.
But if the goal is to provide some kind of converter to automatically take
an old application written with an old language (using GOTO), and generating
python code, it would certainly a great help to have this (unclean) feature
in native python.
Best regards
jm
 
N

Nick Craig-Wood

Grant Edwards said:
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.

Yes I'd agree with that. No more 'goto out'.

There is this also

for (i = 0; ...)
{
if (something)
goto found;
}
/* do stuff when not found */
found:;

(yes I hate setting flags in loops ;-)

This translates exactly to the for: else: construct

for i in xrange(...):
if something:
break
else:
# do stuff when not found

The last language I saw with this very useful feature was FORTH in
about 1984!
 
J

Jeff Shannon

jean-michel said:
Hi all,
I saw a lot of comments saying GOTO is not usefull, very bad, and we
should'nt use it because we don't need it.
I think that's true, but only if you *create* programs.
But if the goal is to provide some kind of converter to automatically take
an old application written with an old language (using GOTO), and generating
python code, it would certainly a great help to have this (unclean) feature
in native python.

But an automatic translator is certain to be imperfect. One can no
more translate mechanically between computer languages than one can
translate mechanically between human languages -- and we've all seen
the fun that can be had by machine-translating from language A ->
language B -> language A, right? What do you think the effect of that
sort of meaning-drift would be on application code?

In other words, any translation from one language to another will
require significant human attention, by someone familiar with both
languages, to ensure that the original meaning is preserved as close
as possible. You're going to have to rewrite chunks of code by hand
no matter what you do; it'd be silly to *not* take that opportunity to
purge things like GOTO.

Jeff Shannon
Technician/Programmer
Credit International
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top