[Tutor] Re: 4 begginers questions ... (fwd)
missive at hotmail.com
Sun Nov 9 09:48:53 EST 2003
>1.) I was puting in some wrong values, and python start to calculating
>endlessly, so I would like to know, if it is possible, how to force python
>to stop executing my instructions in that cases (without quiting program)
Are you typing at the interactive interpreter? (the >>> prompt?)
>2.) And second, after error message, is python continuing from last line,
>or do I need to retype all from the begging of current program (I suppose
>exept variables, which were set previous)
If your program is starting to get complex enough that you are
concerned about "typing it again" you should be saving it in a file
and running it, rather than just typing it at the interactive interpreter
If you expect you might get an error, you can plan for it:
for denominator in range(-10, 10):
quotient = 10.0 / denominator
print '10 / %s = %s' % (denominator, quotient)
print 'cannot divide by zero'
>3.) In your (Alan Gauld) tutorial in part "Conversing with the user" it
>>>>print raw_input("Type something: ")
>As you see raw_input simply displays the given prompt and captures whatever
>the user types in response. Print then displays that response. We could
>instead assign it to a variable:
>resp = raw_input("What's your name? ")
>print "Hi, %s, nice to meet you" % resp
>>>>resp = raw_input("What's your name?")
>What's your name? David # right after this line wich display promt
>and response to that promt - so what is then with ... 'David'
Isn't that the part that you typed? If it did not show that, you would not
be able to see what you were typing.
>... so actually there is no need (and impossible though) to use print
>command again to display that response, (the response is displayed
>automaticly, right after second "promt/response" line) so response is
>"captured" only if we set the answer to variable (like in second case) ...
Right. This actually relates to your next question.
Some functions return a value. What happens to that value is slightly
different depending on whether you are typing at the interactive
interpreter, or running a program from a file.
>4.) Different between :
>>>>a = "dav"
>>>>print a # type print a and hit return ...
>'dav' # dav is displayed inside ' ' sign
>>>>a = "dav"
>>>>a # type a and hit return in this case ...
>dav # dav is displayed without ' ' signs
The interactive interpreter will show you what a name is attached to
if you just type it in and hit enter:
however, if you did the same thing in a program, you would never
see that output:
#save this as showa.py
a = 'dav'
print 'this is a'
print 'this is print a'
now run that program and you will see:
this is a
this is print a
Add photos to your messages with MSN 8. Get 2 months FREE*.
More information about the Tutor