Code Point
September 08, 2010, 08:15:38 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: Geshi Sintax Hilighting Mod Installed. Tens of languages available.
 
   Home   Help Search Login Register  
Pages: [1]   Go Down
  Print  
Author Topic: Tutorial - Control Flow  (Read 2294 times)
flebber
Jr. Member
*

Karma: 0
Offline Offline

Posts: 4


« on: April 01, 2007, 03:48:02 AM »

I was working theough the exercises at http://programming-crash-course.com/play_it_again_sam . I am having a simple problem solving what I thought would be just adding a chained conditional.

The intial code from there

Code
print
 
# as the condition for this while loop is always true
# it will execute forever unless...
#
while True:
 
   mpg = float(raw_input("What is the mileage? Enter 0 to stop: "))
   print
 
   # here is the unless part.
   # if the user enters the value 0 the while loop will be broken
   #
   # the comparison operator is two equal signs "=="
   # not only one "="
   #
   if mpg == 0:
 
       # the if block also has to be indented
 
       print 'The end...'
       print
 
       # the break instruction breaks the loop,
       # be it a while loop or a for loop
       #
       break
 
   # here in instead of building a list ourselves
   # we let the range function build it for us
   #
   gallon_price_list = range(220, 250, 5)
 
   for gallon_price in gallon_price_list:
 
       cost = gallon_price / mpg
       print 'gallon price in US$', gallon_price / 100.0,
       print "cost in US$", cost, "per hundred miles"
 
   print
 
The exercise was to change it to do this Proposed exercise

Break the while loop if the mileage is equal or lower than 0 or if the mileage is bigger than 100.

So I though I could just change the section

 if mpg == 0:

to be

if 0 >= mpg >= 100

but that didn't work I tried many variations on that. Do I have to change the while true statement to be a if, elif, elif. statement.

I figured there must be a simple solution but I can't just see it.
« Last Edit: April 01, 2007, 10:29:49 AM by Alter Lobo » Logged
Alter Lobo
Global Moderator
Jr. Member
*****

Karma: 10
Offline Offline

Posts: 65


« Reply #1 on: April 01, 2007, 10:36:32 AM »

You can change that line to:

Code
    if mpg <= 0 or mpg > 100:
 

Note the "or" operator.
Logged
flebber
Jr. Member
*

Karma: 0
Offline Offline

Posts: 4


« Reply #2 on: April 01, 2007, 11:22:43 AM »

bugger, bugger, bugger, I knew it was so simple. Cheesy . Thanks very much for the reply you've kept a few more hairs on my head, and I am losing a few so I owe you one.
Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!