Give number or “end”

In a previous post I used the following code to prompt the user to give a valid number or the string “end”.

given = input("If you want to stop give end:")
  if given == 'end':
    break
  given = -1
  while given < 0:
    given = input("Give number:")     
    given = int(given)

This code bugged me because there were two prompts, so I replaced it with this one:

given = -1
while not(given >0 and given <= 100):
  given = input("Give number, or end to stop):")
  try:
    given = int(given)
  except ValueError:
    if given == "end":
      break
    else:
      continue
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s