Considering taking a hammer to the computer...

Discussion in 'Python' started by worldsbiggestsabresfan@gmail.com, Dec 31, 2012.

  1. Guest

    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!
    , Dec 31, 2012
    #1
    1. Advertising

  2. MRAB Guest

    On 2012-12-31 23:42, 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!
    >

    Firstly, the second 'while' loop repeats while number_of_floors > 1,
    but the number of floors is never changed within that loop, so the loop
    will repeat forever. Also, if there's only one floor, it won't perform
    the body of the loop at all!

    Secondly, you're not calculating any totals for the final section to
    use.
    MRAB, Jan 1, 2013
    #2
    1. Advertising

  3. On Tue, Jan 1, 2013 at 10:42 AM, <> wrote:
    > 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: "))


    You have a loop here that can never terminate, because
    number_of_floors never changes.

    There are a couple of solutions to this. One would be to compare
    floor_number to number_of_floors, and stop the loop once the one
    exceeds the other; another (and more Pythonic) way would be to use a
    'for' loop, and iterate over the range of numbers from 1 to the number
    of floors. See if your son has learned about range(), if so he should
    be able to figure it out from that clue.

    One tip: When you're asking a question like this, mention what Python
    version you're using. I'm guessing it's Python 3.something, but that
    might not be right. If it is indeed Python 3, then the repeated
    question here will be a problem:

    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: ")

    Note the difference between the two request lines (other than the
    prompt, which is insignificant). The second time around, you're not
    turning it into an integer, so that will crash (in Python 3) with the
    error that strings and integers aren't ordered (that is, that it makes
    no sense to ask whether a string is less than the integer 1). Python
    2, on the other hand, will behave very differently here, as input()
    has a quite different meaning (and one that you almost certainly do
    NOT want).

    Chris Angelico
    Chris Angelico, Jan 1, 2013
    #3
  4. On Tue, Jan 1, 2013 at 10:42 AM, <> wrote:
    > Hey :)


    Oh, and another tip. Threatening violence to your computer is unlikely
    to make it change its ways, and it certainly isn't a helpful subject
    line :)

    All the best.

    ChrisA
    Chris Angelico, Jan 1, 2013
    #4
  5. On 12/31/2012 06:42 PM, 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/
    Mitya Sirenef, Jan 1, 2013
    #5
  6. On 12/31/2012 07:29 PM, Mitya Sirenef wrote:
    >
    >
    > 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
    >
    >


    I forgot to add this:

    question = "How many floors are in the hotel?: "
    number_of_floors = int(input(question))

    while number_of_floors < 1:
    print("Invalid input!")
    number_of_floors = int(input(question))


    It's easier to save the question in a variable and use it two
    times (and do the same in the next loop); it's not clear
    why/if the questions should be different as you're asking
    the user for the same thing.

    -m

    --
    Lark's Tongue Guide to Python: http://lightbird.net/larks/
    Mitya Sirenef, Jan 1, 2013
    #6
  7. 2013/1/1 <>:
    > 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!
    > --
    > http://mail.python.org/mailman/listinfo/python-list


    Hi,
    if "break" isn't allowed, you can add the appropriate condition to the
    while construct
    >>> i=0
    >>> while i < 4:

    .... print i
    .... i = i + 1
    ....
    0
    1
    2
    3
    >>>


    or you can use the for-loops based on the previously determined number
    of the floors and rooms respectively.
    let's hope "range(...)" is allowed - the usual idiom is e.g.:
    >>> for i in range(4):

    .... print i
    ....
    0
    1
    2
    3
    >>>


    Note, that the indexing in python is zero-based (which also applies
    for range by default); the range doesn't include the given upper
    stop-value
    http://docs.python.org/release/3.3.0/library/stdtypes.html#range

    Depending on the assignment and on the interpretation of the
    ground-floor ("zeroth-floor"), you may need to account for this (you
    can also pass the "start" value to range(...) ).

    the totals can be collected simply by incrementing the respective
    numbers with each floor within the loop.

    hth,
    vbr
    Vlastimil Brom, Jan 1, 2013
    #7
  8. Guest

    Here is what I've learned:

    1) There's a bunch of extremely helpful and wonderful people here.

    2) There's a bunch of very intelligent people here.

    3) I still don't have any idea what I'm doing.

    4) It's New Year's Eve and I'm trying to learn Python...?

    I'm going to read all of this over and over until it makes sense to me! Thank you all SO MUCH!!!
    , Jan 1, 2013
    #8
  9. On 12/31/2012 08:30 PM, wrote:
    > Here is what I've learned:
    >
    > 1) There's a bunch of extremely helpful and wonderful people here.
    >
    > 2) There's a bunch of very intelligent people here.
    >
    > 3) I still don't have any idea what I'm doing.
    >
    > 4) It's New Year's Eve and I'm trying to learn Python...?
    >
    > I'm going to read all of this over and over until it makes sense to me! Thank you all SO MUCH!!!



    You're welcome and don't hesitate to ask follow-up question,
    Happy new year!

    - mitya


    --
    Lark's Tongue Guide to Python: http://lightbird.net/larks/
    Mitya Sirenef, Jan 1, 2013
    #9
  10. Modulok Guest

    > 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!



    Here's your program with some extra comments to get you started:

    #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: ")
    # Remember you need to make sure this is an int, just like before.
    # number_of_floors = int(input("Enter the number of floors
    in the hotel: ")) Right now it's a string.


    while number_of_floors > 1:
    # This loop runs forever, as number_of_floors never changes. You need
    # to do something to `number_of_floors` such as de-increment it e.g:
    # `number_of_floors -= 1`, that way we will *eventually* have
    # number_of_floors less than 1, thus stopping the loop. A better
    # idea would be to use a `for` loop instead of the above `while`
    # loop. For example::
    #
    # for i in range(number_of_floors):
    # # blah... do something for each floor. This loop
    auto-terminates.
    #

    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!")
    # You might consider telling your user why their input is
    # invalid. e.g: "rooms on floor must be greater than 10".

    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)


    -Modulok-
    Modulok, Jan 1, 2013
    #10
  11. Tim Chase Guest

    On 12/31/12 19:30, wrote:
    > Here is what I've learned:

    [snip]
    > 4) It's New Year's Eve and I'm trying to learn Python...?


    Can't think of a much better way to spend New Year's Eve, unless
    you're learning Python while also watching fireworks. :)

    -tkc
    Tim Chase, Jan 1, 2013
    #11
  12. On Mon, 31 Dec 2012 22:41:54 -0600, Tim Chase
    <> declaimed the following in
    gmane.comp.python.general:

    > On 12/31/12 19:30, wrote:
    > > Here is what I've learned:

    > [snip]
    > > 4) It's New Year's Eve and I'm trying to learn Python...?

    >
    > Can't think of a much better way to spend New Year's Eve, unless
    > you're learning Python while also watching fireworks. :)
    >

    Using Python to control a fireworks launch sequencer?
    --
    Wulfraed Dennis Lee Bieber AF6VN
    HTTP://wlfraed.home.netcom.com/
    Dennis Lee Bieber, Jan 1, 2013
    #12
  13. Guest

    OK, thank you all for your help yesterday!

    Here's where we are today (using python 3.3.0)

    He has everything running well except for the final calculations - he needsto be able to total the number of rooms in the hotel, as well as the number of occupied rooms. We have tried several different things and can't comeup with a successful command. Any help you can give will be much appreciated!!

    Here's what he's got:

    #This program will calculate the occupancy rate of a hotel
    floor_number = 0
    rooms_on_floor = 0
    occupied_rooms = 0
    total_rooms = 0
    total_occupied = 0



    number_of_floors = int(input("How many floors are in the hotel?: "))
    while number_of_floors < 1:
    print ("Invalid input! Number must be 1 or more")
    number_of_floors = int(input("Enter the number of floors in the hotel: "))

    for i in range(number_of_floors):
    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! Number must be 10 or more")
    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)

    #CALCULATE OCCUPANCY RATE FOR HOTEL
    print()
    total_rooms = sum(rooms_on_floor) #DOESN'T WORK!
    total_occupied = sum(occupied_rooms) #DOESN'T WORK!
    hotel_occupancy = total_occupied / total_rooms
    vacant_rooms = total_rooms - total_occupied
    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)
    print ("The number of vacant rooms at this hotel is ",vacant_rooms)
    , Jan 1, 2013
    #13
  14. Dave Angel Guest

    On 01/01/2013 03:14 PM, wrote:
    > OK, thank you all for your help yesterday!
    >
    > Here's where we are today (using python 3.3.0)
    >
    > He has everything running well except for the final calculations - he needs to be able to total the number of rooms in the hotel, as well as the number of occupied rooms. We have tried several different things and can't come up with a successful command. Any help you can give will be much appreciated!!

    There are two ways to get those totals, depending on whether you know
    how to work lists or not. If you do, then you should make a list of
    occupied_rooms, and sum() it at the end, and make a list of
    rooms_on_flow, and sum that at the end.

    But as you discovered, sum() won't work on an int (BTW, you should give
    the entire traceback instead of saying "doesn't work". In this case, it
    was obvious, but it might not be.)

    On the other hand, if you don't know what a list is, then you need to
    accumulate those numbers as you go. Either way, you need extra
    variables to represent the whole hotel, and you need to do something
    inside the loop to adjust those variables.



    --

    DaveA
    Dave Angel, Jan 1, 2013
    #14
  15. Matt Jones Guest

    rooms_on_floor is being set by the manual input for each floor iterated
    over in your for loop. My guess is your total_rooms value equals the rooms
    from the last floor you processed. Same goes for the occupied_rooms.
    You'll want a separate variable to increment after each occupied_rooms or
    rooms_on_floor is received from the user.

    something like...:

    rooms_on_floor = int(input("Enter the number of rooms on floor: "))
    total_rooms += rooms_on_floor

    *Matt Jones*


    On Tue, Jan 1, 2013 at 2:14 PM, <> wrote:

    > OK, thank you all for your help yesterday!
    >
    > Here's where we are today (using python 3.3.0)
    >
    > He has everything running well except for the final calculations - he
    > needs to be able to total the number of rooms in the hotel, as well as the
    > number of occupied rooms. We have tried several different things and can't
    > come up with a successful command. Any help you can give will be much
    > appreciated!!
    >
    > Here's what he's got:
    >
    > #This program will calculate the occupancy rate of a hotel
    > floor_number = 0
    > rooms_on_floor = 0
    > occupied_rooms = 0
    > total_rooms = 0
    > total_occupied = 0
    >
    >
    >
    > number_of_floors = int(input("How many floors are in the hotel?: "))
    > while number_of_floors < 1:
    > print ("Invalid input! Number must be 1 or more")
    > number_of_floors = int(input("Enter the number of floors in the hotel:
    > "))
    >
    > for i in range(number_of_floors):
    > 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! Number must be 10 or more")
    > 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)
    >
    > #CALCULATE OCCUPANCY RATE FOR HOTEL
    > print()
    > total_rooms = sum(rooms_on_floor) #DOESN'T WORK!
    > total_occupied = sum(occupied_rooms) #DOESN'T WORK!
    > hotel_occupancy = total_occupied / total_rooms
    > vacant_rooms = total_rooms - total_occupied
    > 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)
    > print ("The number of vacant rooms at this hotel is ",vacant_rooms)
    >
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >
    Matt Jones, Jan 1, 2013
    #15
  16. Guest

    That's it!!! Thank you, Matt!! project done! :)

    Thank you all, very much.

    Happy New Year!
    , Jan 1, 2013
    #16
  17. Guest

    That's it!!! Thank you, Matt!! project done! :)

    Thank you all, very much.

    Happy New Year!
    , Jan 1, 2013
    #17
  18. On Wed, Jan 2, 2013 at 7:14 AM, <> wrote:
    > floor_number = 0
    > for i in range(number_of_floors):
    > floor_number = floor_number + 1


    Matt's already given you the part you need (and it seems to have
    worked for you, yay!). Side point: Are you aware that i and
    floor_number are always going to have the same value? You can simplify
    this down to:

    for floor_number in range(number_of_floors):

    ChrisA
    Chris Angelico, Jan 1, 2013
    #18
  19. Dave Angel Guest

    On 01/01/2013 05:01 PM, Chris Angelico wrote:
    > On Wed, Jan 2, 2013 at 7:14 AM, <> wrote:
    >> floor_number = 0
    >> for i in range(number_of_floors):
    >> floor_number = floor_number + 1

    > Matt's already given you the part you need (and it seems to have
    > worked for you, yay!). Side point: Are you aware that i and
    > floor_number are always going to have the same value? You can simplify
    > this down to:
    >
    > for floor_number in range(number_of_floors):
    >
    > ChrisA


    Actually, floor_number is one higher. The easiest way to fix that is to
    adjust the range() parms.

    for floor_number in range(1, 1+number_of_floors):

    Naturally, in some hotels that might not have any rooms on one of the
    floors. Divide by zero.


    --

    DaveA
    Dave Angel, Jan 1, 2013
    #19
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Edwin Dankert

    [ANN] XML Hammer 1.0 beta-2 released

    Edwin Dankert, Oct 18, 2006, in forum: XML
    Replies:
    0
    Views:
    335
    Edwin Dankert
    Oct 18, 2006
  2. Edwin Dankert

    [ANN] XML Hammer 1.0 rc-1 Released

    Edwin Dankert, Jun 27, 2007, in forum: XML
    Replies:
    0
    Views:
    342
    Edwin Dankert
    Jun 27, 2007
  3. Edwin Dankert
    Replies:
    7
    Views:
    469
    Peter Flynn
    Oct 6, 2007
  4. Edwin Dankert
    Replies:
    0
    Views:
    399
    Edwin Dankert
    Jan 23, 2008
  5. Sean McIlroy

    considering taking the plunge

    Sean McIlroy, Sep 15, 2008, in forum: C++
    Replies:
    4
    Views:
    355
Loading...

Share This Page