[Tutor] figured it out
denis
denis.spir at free.fr
Fri Apr 23 06:56:32 EDT 2004
----- Original Message -----
From: <Simonjthecat at aol.com>
To: <tutor at python.org>
Sent: Friday, April 23, 2004 6:21 AM
Subject: [Tutor] figured it out
> Ok... with a little trial and error I figured it out by myself (yay! this
is
> my first working program!) I'll go ahead and put what I came up w/....
Is
> this the most efficient way? It seemed kinda weird to define shape as the
same
> thing twice but it works.
> Here it is.
>
> import sys #for exit command
> shape = raw_input("Which shape[1,2,3,quit]? ")
>
> while shape != 'quit':
> print """
> Choose a shape from the list:
> 1) Triangle
> 2) Square
> 3) Circle
> """
> # note must now test for character '1' not
> # number 1 coz raw_input returns strings not numbers
> if shape == '1': # a triangle
> ht = input('What is the height of your triangle? ')
> base = input('How long is the base? ')
> print "The triangle's area is: ", 0.5*base*ht
>
> elif shape == '2': # square
> side = input("How long are the square's sides? ")
> print "The square's area is: ", side*side
>
> elif shape == '3': # a circle
> rad = input('What radius is your circle? ')
> print "The circle's area is: ", 3.14159*rad*rad
>
> elif shape == 'quit':
> sys.exit()
>
> else:
> print "Sorry, You didn't enter a valid choice"
>
> shape = raw_input("Which shape[1,2,3,quit]? ")
In fact here your start condition "shape != 'quit'" will never be tested
True, even if looks like it is what lets the program exit the loop. As soon
as the user chooses 'quit', the program sys.exits . All right ? You could
have written "while True" or "while 1" instead. You force a hard loop exit
whit the program end.
Thus, you have to choose between sys.exit or standard program ending with
the last instruction in the file ; and between loop exit by testing shape !=
'quit' at the start.
Two more notes :
* fix the user interaction default(s)
* find a way of cancelling the first shape = raw_input() outside the loop
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
More information about the Tutor
mailing list