Considering taking a hammer to the computer...

Mitya Sirenef msirenef at lightbird.net
Tue Jan 1 01:29:34 CET 2013


On 12/31/2012 06:42 PM, worldsbiggestsabresfan at gmail.com wrote:
> Hey :)
 >
 > I'm trying to help my son with an assignment and spending hours 
making an inch of progress. I know nothing about programming and I'm 
trying to learn, on my own, at a rate faster than possible. I would love 
a little help!
 >
 > My son is taking an introductory course and his assignment is to use 
the loops for and while to create a program which calculates a hotel's 
occupancy rate. He has managed all of the "inputs" but needs help with 
the following:
 >
 > 1) The first question asked is how many floors are in the hotel - and 
then the questions are asked floor by floor. We can't figure out how to 
get the program to stop questioning when the number of floors is reached.
 >
 > 2) He has programmed specific calculations for each floor, and now 
needs to have calculations for the entire hotel based on the input about 
each floor.
 >
 >
 > Here is what he has done so far:
 >
 >
 > #This program will calculate the occupancy rate of a hotel
 > floor_number = 0
 >
 >
 > number_of_floors = int(input("How many floors are in the hotel?: "))
 > while number_of_floors < 1:
 > print ("Invalid input!")
 > number_of_floors = input("Enter the number of floors in the hotel: ")
 > while number_of_floors > 1:
 > floor_number = floor_number + 1
 > print()
 > print ("For floor #",floor_number)
 > rooms_on_floor = int(input("How many rooms are on the floor ?: " ))
 > while rooms_on_floor < 10:
 > print ("Invalid input!")
 > rooms_on_floor = int(input("Enter the number of rooms on floor: "))
 >
 > occupied_rooms = int(input("How many rooms on the floor are 
occupied?: "))
 >
 > #CALCULATE OCCUPANCY RATE FOR FLOOR
 > occupancy_rate = occupied_rooms / rooms_on_floor
 > print ("The occupancy rate for this floor is ",occupancy_rate)
 >
 >
 >
 > The following is what we believe needs to go in the program at the 
end except we can't figure out how to calculate it and make it all work 
:/ (alot of the terms have nothing at all to identify them yet...)
 >
 > hotel_occupancy = total_occupied / total_rooms
 > print ("The occupancy rate for this hotel is ",hotel_occupancy)
 > print ("The total number of rooms at this hotel is ",total_rooms)
 > print ("The number of occupied rooms at this hotel is ",total_occupied)
 > vacant_rooms = total_rooms - total_occupied
 > print ("The number of vacant rooms at this hotel is ",vacant_rooms)
 >
 > We've searched and read and we found things about the "break" and 
"pass" commands but his teacher will not allow them because they haven't 
been taught yet.
 >
 > If you have any ideas and can take a minute to help, that would be 
great :)
 >
 > Thank you!


Hi! First I want to note that this task would be easier and better to do
with a break statement, so it's quite unfortunate that the teacher did
not cover the right tools (and very basic ones, in fact) and yet given
this task.

Another question: are you allowed to use functions? (I'm guessing not).

You can do this task much easier if you write it out in pseudo code
before you go to python code. For example, to convert your existing
code to pseudo code:

* set floor_number to 0
* get number of floors from the user

* as long as number of floors is less than 1:
     * print invalid input
     * get number of floors from the user

* as long as number of floors is more than 1:
     * increment floor_number

     * get number of rooms
     * as long as number of rooms is less than 10:
         * get number of rooms

     * get occupied_rooms
     * occupancy_rate = occupied rooms / number of rooms

     * how do we keep track of total rooms and total occupied rooms here??


Does it make it easier to think about the logic of the program?

  - mitya



-- 
Lark's Tongue Guide to Python: http://lightbird.net/larks/




More information about the Python-list mailing list