Created
January 16, 2019 06:46
-
-
Save ChantalDemissie/62b0b377073fab1dd3c7aea11fbca6aa to your computer and use it in GitHub Desktop.
day 3 exercises
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#1 | |
puts "Lets play a guessing game, Please enter a number from 0-999!" | |
number = (rand (0...1000)) | |
i = 0 | |
guess = -1 | |
until guess == number | |
guess = gets.chomp.to_i | |
if guess > number | |
puts "lower" | |
elsif guess < number | |
puts "higher" | |
else guess == number | |
puts "You guessed it! the odds were against you! great job" | |
end | |
end | |
# 2 | |
puts "Lets play a round of Duck Duck Goose!" | |
number = ((1...5)) | |
puts "We have five players!\nWhich player number do you want to goose?" | |
goose = gets.chomp.to_i | |
if goose > 5 | |
puts "We dont have enough players for that" | |
end | |
5.times do |counter| | |
print "Player ##{counter + 1}:" | |
if counter == goose | |
puts "Goose" | |
else | |
puts "Duck" | |
end | |
end | |
# 3 | |
puts "Enter the number of petals on a flower" | |
petals = gets.chomp.to_i | |
(1..petals).each do |counter| | |
print "Plucking petal ##{counter}: " | |
if counter % 2 == 1 | |
puts "they love me" | |
else | |
puts "they love me not" | |
end | |
end | |
#4 | |
puts "Hello! We are going to total some numbers!" | |
puts "Enter a negative number to quit." | |
total = 0 | |
loop do | |
input = gets.chomp.to_i | |
second_input = gets.chomp.to_i | |
if input == second_input | |
if input < 0 | |
break | |
else | |
total += input | |
end | |
end | |
end | |
puts "Result: #{total}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Good work, Chantal!
In exercise 1, how could you update the code to keep track of the number of guesses the user took to guess the number and then share it with the user when they guess the correct number?