473,770 Members | 4,544 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.
Jul 18 '05
25 4650
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:
Bruno Desthuilliers wrote:
Duncan Booth a écrit :
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" )

*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 8-)

Jul 18 '05 #11
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 ?

Jul 18 '05 #12
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('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

Jul 18 '05 #13
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

Jul 18 '05 #14
BOOGIEMAN wrote:
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(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
Jul 18 '05 #15
On Windows, I use WConio
http://newcenturycomputers.net/projects/wconio.html

It provides other screen functions you might have gotten used to in
QBasic as well.

On unix/cygwin, use curses.

I am not aware of any portable library though.
I used to use cls a lot in my QBasic days. Now I just don't.

Jul 18 '05 #16
Harlin wrote:
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").

Jul 18 '05 #17
"BOOGIEMAN" <BO*********@YA HOO.COM> a écrit dans le message de
news:1i******** *************** ******@40tude.n et...
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
Jul 18 '05 #18
Grant Edwards <gr****@visi.co m> wrote:
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!

--
Nick Craig-Wood <ni**@craig-wood.com> -- http://www.craig-wood.com/nick
Jul 18 '05 #19
jean-michel wrote:
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

Jul 18 '05 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
1577
by: Philip Smith | last post by:
I've read with interest the continuing debate about 'lambda' and its place in Python. Just to say that personally I think its an elegant and useful construct for many types of programming task (particularly number theory/artificial intelligence/genetic algorithms) I can't think why anyone would be proposing to do away with it. Sometimes an anonymous function is just what you need and surely it just reflects the python philosophy of...
3
12799
by: Tim Stokes | last post by:
Hello all, Is it possible to make an asp script 'wait' inbetween commands? A quick example (but not what I've got in mind): Response.write ("Please wait. Validating password...") Response.redirect ("done.asp")
51
13388
by: WindAndWaves | last post by:
Can anyone tell me what is wrong with the goto command. I noticed it is one of those NEVER USE. I can understand that it may lead to confusing code, but I often use it like this: is this wrong????? Function x select case z
24
3649
by: sureshjayaram | last post by:
In some functions where i need to return multiple error codes at multiple places, I use multiple return statements. Say for ex. if (Found == 1) { if (val == -1) return error1; } else { if (val2 == -1)
6
3652
by: eBob.com | last post by:
How do you make a loop iterate early without using a GoTo? (I guess I've done too much structured programming and I really don't like using GoTos.) Here's my code ... For Each Thing As OFI In FileInfo If Thing.Displayed <> True Then GoTo Iterate 'skip this entry; try next one End If
5
11366
by: BF | last post by:
Hi, I have a problem: I am writing an update script for a database and want to check for the version and Goto the wright update script. So I read the version from a table and if it match I want to "Goto Versionxxx" Where Versionxxx: is set in the script with the right update script.
3
3277
by: memyselfandmiah | last post by:
I am in a cpp class, but i am messing around the program on my own, and I am looking for some commands. or at least a place that i can go to find commands that i want. There are 2 questions that I currently have. When i started with coding (way back on QBASIC) there were 2 commands that it seems like should be somewhat universal. one was a wait function. IE, in a loop function a command that makes the program wait for time N before it...
19
15896
by: David Given | last post by:
I have a situation where I need to use GOTO in a Javascript program. No, I can't use break or continue. Yes, I really do know what I'm doing. What I've got is a huge mass of automatically generated code that contains thousands of clauses like this: label1: ...do something... if (condition) goto label3;
59
5065
by: raashid bhatt | last post by:
why are GOTO's not used they just a simple JMP instructions what's bad about them
0
9591
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9425
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10228
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10057
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9869
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5312
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5449
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3575
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2816
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.