Exercise 1
Write a program that allows a user to play a guessing number game. Your program should generate a random number between 0 – 1000 (including 0, but not including 1000). Allow the user to make a guess until they guess the answer. After each guess you should print “higher” or “lower”. When they guess it correctly print a winning message along with their total number of guesses.
My solution:
Since 1000 is excluded, I used ...
for the range.
(Note: Pretty sure rand(1000)
does the same thing, but I wanted to practice the syntax covered in JSL.)
The random number generated by rand()
is assigned to rand_number
.
User input is converted into an integer and assigned to guess
. I initialized a variable named number_of_guesses
with a value of 1.
Next, I created a while
loop. While guess
does NOT equal rand_number
, the loop will continuously run.
Inside the loop, an if
-statement checks whether guess
is higher or lower than rand_number. Depending on which statement is true
, the program prints a helpful hint (HIGHER or LOWER) for the user.
After printing the hint, the program collects input from the user again. The new value is stored in guess
.
Next, number_of_guesses
increments by 1
with a compound assignment statement.
When the user guesses the correct number, they receive a congratulatory message. It tells them how many guesses they made.
Sample run:
Exercise 2
Write a program that plays duck duck goose. Allow the user to enter the player’s number they want to call goose on, and then say “duck” for each player before the “goose”, then say “goose” for the chosen player.
My solution:
First, I print a message for the user. Then, I convert user input to an integer — and assign it to variable goosed_player
.
Next, I use range.each
for the duck-duck-duck” portion of the game. The range is between 1
and the number assigned to goosed_player
. However, ...
excludes goosed_player
from the loop.
Since each number represents a player, I assign the variable player
to the current iterator.
For every player (except the one being Goosed), it prints “Duck”.
The loop stops before it reaches the value assigned to goosed_player
. Then, I print a message that “Gooses” whichever player the user selected.
Sample run:
Exercise 3
Write a program that allows a user to enter the number of petals on a flower. Then one by one, print “plucking petal #1: they love me!”. Alternate “They love me” and “They love me not” as well as increase the petal number for each petal.
My solution:
User input is collected, converted to an integer, and then assigned to the variable named petals
.
An each loop iterates over a range of 1
to the value stored inside petals
. This range is inclusive.
The current iterator is assigned to the variable num
.
Inside the loop, an if
-statement evaluates what to print. If the current num
is odd, it prints “They love me!” If the current num
is even, it prints “They love me not.”
The loop ends after it prints the statement associated with the number stored inside petals
.
Sample run:
Exercise 4
You don’t trust your users. Modify the program below to require the user to enter the same value twice in order to add that value to the total.
Original code:
My solution:
First, I initialized a third variable named verification
— setting it at 0
.
Next, I changed the while
loop to an until
loop.
Why? Personally, until
made it easier for me to imagine what conditions to place on the loop — especially since I’m now juggling a second condition.
(Note: I certainly could have used while
, but with inverted conditions instead.)
My program checks if input <= -1
and input == verification
both evaluate as true
.
In other words, I’m telling Ruby: “Until input
is -1
or less, AND input
equals verification
, keep running this loop.”
The loop makes the user give input twice. One value is assigned to input
, the other is assigned to verification
.
Next, an if
-statement evaluates whether to add the user’s number to total
.
If input
and verification
are the same, AND input
isn’t a negative number, then input
is added to the current value stored inside total
.
When the loop ends, the program prints total
.
Sample run:
Summary
These exercises let me practice working with scope, loops and iterators. I also used if-elsif-else
statements and conditionals.
Cool things I learned from JSL Day 3:
In no particular order:
- We can use
.times
as a regular loop or an iterator (with its own variable assigned between pipes) .times
refers to the number of elements inside the collection.range.each
passes a specific range into the code as a collection. This range can either include or exclude the final value.