Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Considering taking a hammer to the computer...

Reply
Thread Tools

Considering taking a hammer to the computer...

 
 
worldsbiggestsabresfan@gmail.com
Guest
Posts: n/a
 
      12-31-2012
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!
 
Reply With Quote
 
 
 
 
MRAB
Guest
Posts: n/a
 
      01-01-2013
On 2012-12-31 23:42, http://www.velocityreviews.com/forums/(E-Mail Removed) 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.

 
Reply With Quote
 
 
 
 
Chris Angelico
Guest
Posts: n/a
 
      01-01-2013
On Tue, Jan 1, 2013 at 10:42 AM, <(E-Mail Removed)> 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
 
Reply With Quote
 
Chris Angelico
Guest
Posts: n/a
 
      01-01-2013
On Tue, Jan 1, 2013 at 10:42 AM, <(E-Mail Removed)> 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
 
Reply With Quote
 
Mitya Sirenef
Guest
Posts: n/a
 
      01-01-2013
On 12/31/2012 06:42 PM, (E-Mail Removed) 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/

 
Reply With Quote
 
Mitya Sirenef
Guest
Posts: n/a
 
      01-01-2013
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/

 
Reply With Quote
 
Vlastimil Brom
Guest
Posts: n/a
 
      01-01-2013
2013/1/1 <(E-Mail Removed)>:
> 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...pes.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
 
Reply With Quote
 
worldsbiggestsabresfan@gmail.com
Guest
Posts: n/a
 
      01-01-2013
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!!!
 
Reply With Quote
 
Mitya Sirenef
Guest
Posts: n/a
 
      01-01-2013
On 12/31/2012 08:30 PM, (E-Mail Removed) 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/

 
Reply With Quote
 
Modulok
Guest
Posts: n/a
 
      01-01-2013
> 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-
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
considering taking the plunge Sean McIlroy C++ 4 09-16-2008 02:43 PM
CDV’s Hammer & Sickle PC Game @ ThinkComputers.org Silverstrand Front Page News 0 12-07-2005 02:49 AM
Hammer>Nail>Head TJ Computer Support 18 04-25-2005 08:55 PM
Hammer Films from Universal and M-G-M Film Buff DVD Video 6 05-08-2004 10:00 AM
Hammer Horror DVD Reviews @ GENRE ONLINE.NET Writer R5 DVD Video 0 05-07-2004 01:43 AM



Advertisments