Write a program that asks the user for all U.S. states they have visited (followed by a return). Typing "done" should terminate the program. Print all states to the screen.
Let's set up an empty array of visited states and prompt the user for input.
visited = []
puts "Enter all the states you've been to! Type 'done' when you're finished:"
Get the states from the user, then check to see if they entered "done". If so stop collecting input, otherwise push to the visited array.
state = gets.chomp.upcase
if state != 'DONE'
visited.push(state)
end
We need a loop to keep collecting states from the user. But...
keep_going = true
while keep_going
state = gets.chomp.upcase
if state != 'DONE'
visited.push(state)
end
end
... we need a way to exit the loop. So set 'keep_going' to false when the user enters done.
keep_going = true
while keep_going
state = gets.chomp.upcase
if state != 'DONE'
visited.push(state)
else
keep_going = false
end
end
Finally we need to print out the states the user has visited. We can either join a comma separated array...
puts "You have visited the following states:"
puts visited.join(', ')
visited.each do |state|
puts state
end
...or loop through and print each state.
# Write a program that asks the user for all U.S. states they have visited
# (followed by a return)
# Typing "done" should terminate the program.
# Print all states to the screen.
visited = []
puts "Enter all the states you've been to! Type 'done' when you're finished:"
keep_going = true
while keep_going
state = gets.chomp.upcase
if state != 'DONE'
visited.push(state)
else
keep_going = false
end
end
puts "You have visited the following states:"
puts visited.join(', ')
Let's walk through this exercise together from scratch!
I like to start simple. First let's set up an array of students, assign it to a variable and print it to the terminal to make sure it's working.
# Write a program that splits the class into teams of 2.
# If there's an odd number of students the last group should be a group of 3.
students = ['Daniele', 'Alesia','Anita',
'Ben', 'Thomas', 'Matthew',
'Mark', 'Jennifer', 'Cameron']
puts students
student_shuffle.rb
It's a good habit to push your code to Github once you get something working. Let's walk through the steps once more.
Sweet! You set up your repo remotely!
Next we have to link it to your local Ruby folder.
$ git init
$ git status
$ git add .
$ git commit -m "first commit"
$ git remote add origin https://github.com/jpanaia/ruby.git
$ git push origin master
Our file is officially stored in version control. Let's practice pushing to Github as we work. You should get in the habit of committing early and often.
GITHUB = RESUME
Back to our code....
Next we want to randomize our student array. There are several ways to do this. Let's use the ".shuffle!" method and test it.
Curious about methods you can use with arrays?
http://www.ruby-doc.org/core-2.2.0/Array.html
students = ['Daniele', 'Alesia','Anita',
'Ben', 'Thomas', 'Matthew',
'Mark', 'Jennifer', 'Cameron']
# shuffle the student array
students.shuffle!
puts students
We got something else working! This is so exciting I think we should push to Github.
$ git status
Let's add to the staging area.
$ git add .
$ git status
We see our file has been staged.
Next step is committing to the local repo w/ a message
$ git commit -m "shuffled student array"
And check the status again.
Finally we'll push to the remote repository.
$ git push origin master
Now we have 2 commits saved.
Let's refocus on our array of students. Using an until loop we can iterate through our array. We can simply print out the count to make sure it's working.
# set an array of students
students = ['Daniele', 'Alesia', 'Anita', 'Ben', 'Thomas', 'Matthew', 'Mark', 'Jennifer', 'Cameron']
# shuffle student array
students.shuffle!
# set a count
count = 0
# until count is the number of students
until count == students.length
# Test loop
puts count
# increase the count
count += 1
end # end until
Now, instead of counting let's print each student.
# set a count
count = 0
# until count is the number of students
until count == students.length
# output each student
puts "#{students[count]}"
# increase the count
count += 1
end # end until
We can add 1 to the array's index to print the next student.
# set a count
count = 0
# until count is the number of students
until count == students.length
# output students in pairs
puts "#{students[count]} & #{students[count+1]}"
# increase the count
count += 1
end # end until
We increase the count by 2 to account for pairs of students. We're very close but still missing that 9th student.
# set a count
count = 0
# number of students
num_students = students.length - 1
# until count is the number of students
until count == num_students
# output students in pairs
puts "#{students[count]} & #{students[count+1]}"
# increase the count
count += 2
end # end until
This is a great time to push to Github. Remember how?
$ git add .
$ git commit -m "paired up students"
$ git push origin master
We'll need to set up a condition to output different results depending on our position in the loop. Once it hits the 6th spot (3 from the end) we assign that group 3 members.
# set an array of students
students = ['Daniele', 'Alesia', 'Anita', 'Ben', 'Thomas', 'Matthew', 'Mark', 'Jennifer', 'Cameron']
# shuffle student array
students.shuffle!
# set a count
count = 0
# number of students
num_students = num_students - 1
until count == num_students
if count < num_students - 3
# output pairs of students
puts "#{students[count]} & #{students[count+1]}"
else
puts "#{students[count]}, #{students[count+1]} & #{students[count+2]}"
end
# increase the count
count += 2
end # end until
BUT...what happens if we only have 8 students instead of 9 (sorry Cam).
# set an array of students
students = ['Daniele', 'Alesia', 'Anita', 'Ben', 'Thomas', 'Matthew', 'Mark', 'Jennifer']
At this point it's clear that we have to test for even and odd numbers of students.
We'll use the modulus (%) operator to
determine evenness / oddness.
If the number of students divided by 2 has a remainder of 1 it's ODD
If the number of students divided by 2 has a remainder of 0 it's EVEN
We can fix the bug by adding a condition for odd vs even number of students.
until count == number_of_students
# odd # students
if number_of_students % 2 == 1
if count < num_students - 3
# output pairs of students
puts "#{students[count]} & #{students[count+1]}"
else
puts "#{students[count]}, #{students[count+1]} & #{students[count+2]}"
break
end
else # even # students
# output pairs of students
puts "#{students[count]} & #{students[count+1]}"
end
# increase the count
count += 2
end # end until
How could we modify this code to take user input instead of hard-coding a student array?
Think back to our warmup exercise...
# empty student array
students = []
student = ''
# prompt user
puts "Who showed up to class today? Type 'done' when finished:"
while student != 'done'
student = gets.chomp
# push student into students array
students.push(student)
end
# pop off last element 'done'
students.pop
# shuffle student array
students.shuffle!
# set a count
count = 0
puts "\nYour teams are:"
until count == students.length
# Odd # students
if number_of_students % 2 == 1
if count < number_of_students - 3
# output pairs of students
puts "#{students[count]} & #{students[count+1]}"
else
puts "#{students[count]}, #{students[count+1]} & #{students[count+2]}"
break
end
else # even # students
# output pairs of students
puts "#{students[count]} & #{students[count+1]}"
end
# increase the count
count += 2
end # end until
new code!
Are you sick of git puns yet?
Go ahead and add, commit and push to Github.
Anyone see the bug in this code? There's one special case that I've completely neglected.
What happens if only one student comes to class?
until count == students.length
# One student
if number_of_students == 1
puts "#{students[0]} is a team of 1"
break
else
# Odd # students
if number_of_students % 2 == 1
# 3 students or more
if count < students.length - 3
# output pairs of students
puts "#{students[count]} & #{students[count+1]}"
else
puts "#{students[count]}, #{students[count+1]} & #{students[count+2]}"
break
end
else # even # students
# output pairs of students
puts "#{students[count]} & #{students[count+1]}"
end
end
# increase the count
count += 2
end # end until
# Write a program that prints the lyrics
# to 99 Bottles of Beer on the Wall
# example output:
# 99 bottles of beer on the wall
# 99 bottles of beer
# Take one down pass it around
#
# 98 bottles of beer on the wall
# 98 bottles of beer on the wall
# 98 bottles of beer
# Take one down, pass it around
Write a program that prints the lyrics to 99 Bottles of Beer on the Wall
# N number of bottles
number_of_bottles = 99
until number_of_bottles == 0
# Do something
end
Let's define the number of bottles to start with (99, duh!). Then we'll loop until we're out of beer!
# N number of bottles
number_of_bottles = 99
until number_of_bottles == 0
# print 2 lines using number of bottles
# print refrain (doesn’t change)
# print a line using number - 1
end
Let's outline our solution
# N number of bottles
number_of_bottles = 99
until number_of_bottles == 0
# print 2 lines using number of bottles
puts "#{number_of_bottles} bottles of beer on the wall"
puts "#{number_of_bottles} bottles of beer"
# print refrain (doesn’t change)
puts "Take one down, pass it around"
#print a line using N -1
puts "#{number_of_bottles - 1} bottles of beer on the wall\n\n"
end
Now print a bunch of strings (with interpolation!) to sing our song. What happens when we run this code?
number_of_bottles = 99
until number_of_bottles == 0
# print 2 lines using N
puts "#{number_of_bottles} bottles of beer on the wall"
puts "#{number_of_bottles} bottles of beer"
# print refrain (doesn’t change)
puts "Take one down, pass it around\n\n"
#print a line using N -1
puts "#{number_of_bottles - 1} bottles of beer on the wall"
number_of_bottles -= 1
end
A pesky infinite loop!
We need to decrement (-=). Now we're pretty close. How would we ensure correct grammar at 1 bottle of beer?
if number_of_bottles == 1
puts "#{number_of_bottles} bottle of beer on the wall"
puts "#{number_of_bottles} bottle of beer"
# print refrain (doesn’t change)
puts "Take it down, pass it around"
puts "No more bottles of beer on the wall!"
else
# print 2 lines using N
puts "#{number_of_bottles} bottles of beer on the wall"
puts "#{number_of_bottles} bottles of beer"
# print refrain (doesn’t change)
puts "Take one down, pass it around"
if number_of_bottles == 2
puts "1 bottle of beer on the wall\n\n"
else
#print a line using N -1
puts "#{number_of_bottles - 1} bottles of beer on the wall\n\n"
end
end
Here it is! We have to add a few special cases.
if number_of_bottles > 1
# print 2 lines using N
puts "#{number_of_bottles} bottles of beer on the wall"
puts "#{number_of_bottles} bottles of beer"
# print refrain (doesn’t change)
puts "Take one down, pass it around"
# print a line using N -1
puts "#{number_of_bottles - 1} bottles of beer on the wall\n\n"
else
# print 2 lines using N
puts "1 bottle of beer on the wall"
puts "1 bottle of beer"
# print refrain (doesn’t change)
puts "Take it down, pass it around"
# print a line using N -1
puts "No more bottles of beer on the wall!"
end
Let's also add logic to ensure correct grammar when we get down to 1 bottle.
We're close here but it still needs more love...
# Ask the user 5 yes or no questions
# Compare their answers to a list of correct answers
# Print the number of correct answers
questions = [ 'Are narwhals real?',
'Is today Halloween?',
'Do dogs say meow?',
'Does 2+2 = 4?',
'Is Jaime awesome?']
correct_answers = ['Y', 'N', 'N', 'Y', 'Y']
puts "Please answer Y or N to the following questions!"
questions.each do |question|
puts "Q: #{question}"
user_answer = gets.chomp.upcase
end
index = 0
score = 0
questions.each do |question|
puts "Q: #{question}"
user_answer = gets.chomp.upcase
if user_answer == correct_answers[index]
# Do Something
end
end
questions.each do |question|
puts "Q: #{question}"
user_answer = gets.chomp.upcase
if user_answer == correct_answers[index]
score += 1
end
index += 1
end
puts "You got #{score}/5 correct answers!"
if user_answer == 'Y' || user_answer == 'N'
if user_answer == correct_answers[index]
score += 1
end
index += 1
else
puts "Try again: Y or N?"
end
questions.each do |question|
puts "Q: #{question}"
user_answer = gets.chomp.upcase
if user_answer == 'Y' || user_answer == 'N'
if user_answer == correct_answers[index]
score += 1
end
index += 1
else
puts "Try again: Y or N?"
redo
end
end
questions = [ 'Are narwhals real?',
'Is today Halloween?',
'Do dogs say meow?',
'Does 2+2 = 4?',
'Is Jaime awesome?']
correct_answers = ['Y', 'N', 'N', 'Y', 'Y']
index = 0
score = 0
puts "Please answer Y or N to the following questions!"
questions.each do |question|
puts "Q: #{question}"
user_answer = gets.chomp.upcase
if user_answer == 'Y' || user_answer == 'N'
if user_answer == correct_answers[index]
score += 1
end
index += 1
else
puts "Try again: Y or N?"
redo
end
end
puts "You got #{score}/5 correct answers!"
questions_answers = {'Are narwhals real?'=> 'Y',
'Is today Halloween?'=> 'N',
'Do dogs say meow?' => 'N',
'Does 2+2 = 4?' => 'Y',
'Is Jaime awesome?' => 'Y'}
score = 0
puts "Please answer Y or N to the following questions!"
questions_answers.each do |question, correct_answer|
puts "Q: #{question}"
user_answer = gets.chomp.upcase
end
if user_answer == 'Y' || user_answer == 'N'
if user_answer == correct_answer
score += 1
end
else
puts "Try again: Y or N?"
redo
end
puts "You got #{score}/#{questions_answers.count} correct answers!"
questions_answers = {'Are narwhals real?' => 'Y',
'Is today Halloween?' => 'N',
'Do dogs say meow?' => 'N',
'Does 2+2 = 4?' => 'Y',
'Is Jaime awesome?' => 'Y'}
score = 0
puts "Please answer Y or N to the following questions!"
questions_answers.each do |question, correct_answer|
puts "Q: #{question}"
user_answer = gets.chomp.upcase
if user_answer == 'Y' || user_answer == 'N'
if user_answer == correct_answer
score += 1
end
else
puts "Try again: Y or N?"
redo
end
end
puts "You got #{score}/#{questions_answers.count} correct answers!"
@questions_answers = {'Are narwhals real?' => 'Y',
'Is today Halloween?' => 'N',
'Do dogs say meow?' => 'N',
'Does 2+2 = 4?' => 'Y',
'Is Jaime awesome?' => 'Y'}
@score = 0
def prompt
puts "Please answer Y or N to the following questions!"
end
def put_score
# Do something
end
def put_score(s,q_a)
puts "You got #{s}/#{q_a.count} correct answers!"
end
def ask_questions
@questions_answers.each do |question, correct_answer|
puts "Q: #{question}"
user_answer = gets.chomp.upcase
if user_answer == 'Y' || user_answer == 'N'
if user_answer == correct_answer
@score += 1
end
else
puts "Try again: Y or N?"
redo
end
end
end
def main
prompt
ask_questions
put_score(@score)
end
main
@questions_answers = {'Are narwhals real?' => 'Y',
'Is today Halloween?' => 'N',
'Do dogs say meow?' => 'N',
'Does 2+2 = 4?' => 'Y',
'Is Jaime awesome?' => 'Y'}
@score = 0
def prompt
puts "Please answer Y or N to the following questions!"
end
def ask_questions
@questions_answers.each do |question, correct_answer|
puts "Q: #{question}"
user_answer = gets.chomp.upcase
if user_answer == 'Y' || user_answer == 'N'
if user_answer == correct_answer
@score += 1
end
else
puts "Try again: Y or N?"
redo
end
end
end
def put_score(s,q_a)
puts "You got #{s}/#{q_a.count} correct answers!"
end
def main
prompt
ask_questions
put_score(@score,@questions_answers)
end
main
Build a quiz program that gets a few inputs from the user including:
Then clear the screen and begin the quiz. Keep score!