Exercises: validation

Tags
Exercise

Zilo's zen rater

Zilo is a self-proclaimed Zen master. Goat Zen is nothing like human Zen. Goat Zen is about purity-of-hide. The more tattoos, the better. Why? Zilo says it came to him in a divine dream.

Write a program to compute a goat's Zen rating, based on the number of tattoos they have.

Tattoos Rating
0 0
More than 0, less than 4 1
4 or more, less than 8 2
8 or more, less than 14 3
14 or more 4

There's one exception. If the goat has a tattoo on its tongue, its zen rating is 5. No need to ask any other questions.

Validation rules:

  • Tongue tattooness must be Y or N, although allow spaces and upper/lowercase.
  • Number of tattoos: Integer, from 0 to 100.

Here's some I/O:

  • Does the goat have a tongue tattoo? (Y=yes, N=no)? y
  •  
  • Zen rating: 5

More:

  • Does the goat have a tongue tattoo? (Y=yes, N=no)? n
  • How many tattoos? 7
  •  
  • Zen rating: 2

Even more.

  • Does the goat have a tongue tattoo? (Y=yes, N=no)? asfd
  • Please enter Y or N.
  • Does the goat have a tongue tattoo? (Y=yes, N=no)? ff
  • Please enter Y or N.
  • Does the goat have a tongue tattoo? (Y=yes, N=no)? y
  •  
  • Zen rating: 5

Yet more.

  • Does the goat have a tongue tattoo? (Y=yes, N=no)? n
  • How many tattoos? asd
  • Sorry, you must enter a whole number.
  • How many tattoos? 2.3
  • Sorry, you must enter a whole number.
  • How many tattoos? -2
  • Sorry, please enter a number zero or more, and 100 or less.
  • How many tattoos? 434
  • Sorry, please enter a number zero or more, and 100 or less.
  • How many tattoos? 2
  •  
  • Zen rating: 1

Upload a zip of your project folder. The usual coding standards apply.

Exercise

More dwarves?

You have some Snow Whites and some dwarves. Each Snow White needs seven dwarves, but you might have more or fewer dwarves than you need. Write a program to figure out how many dwarves to add, or get rid of.

Ask the user for the number of Snow Whites they have, and the number of dwarves. Both must be integers. There must be 1 or more Snow Whites, but not more than 17. There must be 0 or more dwarves, but not more than 93.

Compute the number of dwarfs needed or are extra. Use the output format below.

Here's some I/O.

  • How many snow whites? 2
  • How many dwarfs? 14
  •  
  • Central Casting
  • ======= =======
  •  
  • Dwarf hiring order
  •  
  • You have the right number of dwarfs.

More I/O. Note that singular/plural is handled correctly.

  • How many snow whites? 1
  • How many dwarfs? 6
  •  
  • Central Casting
  • ======= =======
  •  
  • Dwarf hiring order
  •  
  • Hire one more dwarf.

More I/O:

  • How many snow whites? 2
  • How many dwarfs? 16
  •  
  • Central Casting
  • ======= =======
  •  
  • Dwarf hiring order
  •  
  • Get rid of 2 dwarfs.

More I/O:

  • How many snow whites? some
  • Sorry, you must enter a whole number.
  • How many snow whites? 99
  • Sorry, please enter a number one or more, and 17 or less.
  • How many snow whites? 0
  • Sorry, please enter a number one or more, and 17 or less.
  • How many snow whites? 2
  • How many dwarfs? 999
  • Sorry, please enter a number zero or more, and 93 or less.
  • How many dwarfs? a lot
  • Sorry, you must enter a whole number.
  • How many dwarfs? 13.5
  • Sorry, you must enter a whole number.
  • How many dwarfs? 13
  •  
  • Central Casting
  • ======= =======
  •  
  • Dwarf hiring order
  •  
  • Hire one more dwarf.

Hints (you don't need to use them all):

  • The abs() function will take the absolute value of a number. So abs(3) and abs(-3) are both 3.
  • The str() function will convert an int or float into a string.
  • The number of dwarves needed is seven times the number of Snow Whites, minus the number of dwarves you have. If you have too many dwarves, that value will be negative.

If you have the right number of dwarves, the message might be "You have the right number of dwarves."

If you have one too many dwarves, the message might be "Get rid of one dwarf."

If you have way too many dwarves, the message might be "Get rid of [number] dwarves."

Upload a zip of your project folder. The usual coding standards apply.

Exercise

Sheep yurt sizing

Bo Peep is in the yurting biz. There's a big demand for sheep yurts in Mongolia, Manitoba, and other M places.

Bo sells four models, and can make custom yurts, too. Here are the yurts Bo sells:

Model Area
Yurtlet 25 sq meters
Rambler 64 sq meters
Homesteader 100 sq meters
X2000 Industrial 200 sq meters
Custom build More than 200 sq meters

Here are the areas one sheep needs at minimum:

Size Area
Small 0.67 sq meters
Medium 0.83 sq meters
Big 1.15 sq meters

So if you had, say, 10 small sheep, 11 medium, and 8 big, you'd need at least 8 × 1.15 + 11 × 0.83 + 10 × 0.67 = 25.03 sq meters.

You also need a ventilator to prevent methane explosions. There are three models: Sipper (low flow), Gulper (medium flow), and Chugger (blast it!). The Sipper is cheap but less effective. If you use one, you'll need to add 22% to the area needed, so there's space around the sheep for the gas. The Gulper is better, but you'll still need to add 12%. The Chugger is... well, it sucks really well. No extra space needed.

Let's say you used the Gulper with the 10 - 11 - 8 sitch. You need 25.03 * 1.12 = 28 square meters.

What model yurt would the program recommend in this case? A Rambler.

Validate the input. Keep looping until the user enters valid deets. Rules:

  • Small 🐑: integer from 0 to 100.
  • Medium 🐑: integer from 0 to 70.
  • Big 🐑: integer from 0 to 50.
  • Ventilator: S, G, or C. Allow upper/lowercase. Allow extra spaces.

Some I/O:

  • How many small sheep? 10
  • How many medium sheep? 11
  • How many big sheep? 8
  • Ventilator (S=Sipper, G=Gulper, C=Chugger)? g
  •  
  • Area required: 28
  • Recommended model: Rambler

Round Area required to the nearest integer.

More I/O.

  • How many small sheep? some
  • Sorry, you must enter a whole number.
  • How many small sheep? 22.2
  • Sorry, you must enter a whole number.
  • How many small sheep? -1
  • Sorry, please enter a number one or more, and 100 or less.
  • How many small sheep? 39939
  • Sorry, please enter a number one or more, and 100 or less.
  • How many small sheep? 30
  • How many medium sheep? less
  • Sorry, you must enter a whole number.
  • How many medium sheep? 22
  • How many big sheep? 17
  • Ventilator (S=Sipper, G=Gulper, C=Chugger)? g
  •  
  • Area required: 65
  • Recommended model: Homesteader

Even more I/O.

  • How many small sheep? 19
  • How many medium sheep? 14
  • How many big sheep? 9
  • Ventilator (S=Sipper, G=Gulper, C=Chugger)? something
  • Please enter S (Sipper), G (gulper, or C (Chugger).
  • Ventilator (S=Sipper, G=Gulper, C=Chugger)? s
  •  
  • Area required: 42
  • Recommended model: Rambler

Upload a zip of your project folder. The usual coding standards apply.