Considering taking a hammer to the computer...

  • Thread starter worldsbiggestsabresfan
  • Start date
W

worldsbiggestsabresfan

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

MRAB

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.
 
C

Chris Angelico

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
 
C

Chris Angelico


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
 
M

Mitya Sirenef

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
 
M

Mitya Sirenef

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
 
V

Vlastimil Brom

2013/1/1 said:
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,
if "break" isn't allowed, you can add the appropriate condition to the
while construct.... 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.:.... 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
 
W

worldsbiggestsabresfan

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

Mitya Sirenef

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
 
M

Modulok

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

Tim Chase

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
 
D

Dennis Lee Bieber

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?
 
W

worldsbiggestsabresfan

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)
 
D

Dave Angel

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.
 
M

Matt Jones

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*
 
W

worldsbiggestsabresfan

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

Thank you all, very much.

Happy New Year!
 
W

worldsbiggestsabresfan

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

Thank you all, very much.

Happy New Year!
 
C

Chris Angelico

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
 
D

Dave Angel

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top