Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #35862

Re: Considering taking a hammer to the computer...

References <2f5053ab-a646-49d3-a569-61468f518b9f@googlegroups.com>
Date 2013-01-01 11:08 +1100
Subject Re: Considering taking a hammer to the computer...
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.1501.1356998899.29569.python-list@python.org> (permalink)

Show all headers | View raw


On Tue, Jan 1, 2013 at 10:42 AM,  <worldsbiggestsabresfan@gmail.com> 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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Considering taking a hammer to the computer... worldsbiggestsabresfan@gmail.com - 2012-12-31 15:42 -0800
  Re: Considering taking a hammer to the computer... Chris Angelico <rosuav@gmail.com> - 2013-01-01 11:08 +1100
  Re: Considering taking a hammer to the computer... Chris Angelico <rosuav@gmail.com> - 2013-01-01 11:09 +1100
  Re: Considering taking a hammer to the computer... Mitya Sirenef <msirenef@lightbird.net> - 2012-12-31 19:29 -0500
  Re: Considering taking a hammer to the computer... Mitya Sirenef <msirenef@lightbird.net> - 2012-12-31 19:34 -0500
  Re: Considering taking a hammer to the computer... Vlastimil Brom <vlastimil.brom@gmail.com> - 2013-01-01 01:36 +0100
  Re: Considering taking a hammer to the computer... worldsbiggestsabresfan@gmail.com - 2012-12-31 17:30 -0800
    Re: Considering taking a hammer to the computer... Mitya Sirenef <msirenef@lightbird.net> - 2012-12-31 21:00 -0500
    Re: Considering taking a hammer to the computer... Tim Chase <python.list@tim.thechases.com> - 2012-12-31 22:41 -0600
    Re: Considering taking a hammer to the computer... Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-01-01 14:35 -0500
  Re: Considering taking a hammer to the computer... Modulok <modulok@gmail.com> - 2012-12-31 20:59 -0700
  Re: Considering taking a hammer to the computer... worldsbiggestsabresfan@gmail.com - 2013-01-01 12:14 -0800
    Re: Considering taking a hammer to the computer... Matt Jones <matt.walker.jones@gmail.com> - 2013-01-01 14:46 -0600
      Re: Considering taking a hammer to the computer... worldsbiggestsabresfan@gmail.com - 2013-01-01 13:57 -0800
      Re: Considering taking a hammer to the computer... worldsbiggestsabresfan@gmail.com - 2013-01-01 13:57 -0800
    Re: Considering taking a hammer to the computer... Chris Angelico <rosuav@gmail.com> - 2013-01-02 09:01 +1100
    Re: Considering taking a hammer to the computer... Dave Angel <d@davea.name> - 2013-01-01 15:28 -0500
    Re: Considering taking a hammer to the computer... Dave Angel <d@davea.name> - 2013-01-01 17:34 -0500
  Re: Considering taking a hammer to the computer... MRAB <python@mrabarnett.plus.com> - 2013-01-01 00:02 +0000

csiph-web