Stuck on a Ruby exercise that probably has a very simple solution. I just started yesterday :-(


#1

Hi guys,

This question does not apply to a Codecademy exercise - it's from an external source of small exercises I've been playing around with. If that's against the rules I do apologise (although I'm pretty sure it isn't:

"Use these forums to connect with other users and talk about your journey of learning how to code (either on Codecademy or outside of it)"

First off, this is probably going to come across as a really newb question to a lot of you, but please understand I only started learning anything related to Ruby and programming yesterday.

So I'm working on an exercise where I need to do the following:

"Write a program that asks the user three simple sums. Each sum should ask the user to multiply two random numbers, between one and ten. The program should keep score of how many questions the user got correct, and display the score at the end. Your program must contain a while loop that loops three times. The output for your program should look something like this:

>>>>>>>>>> Running: game.rb >>>>>>>>>>>>>>>
Welcome to Super Multiplier!
======================
What is 1 times 7?
7
Correct!
What is 2 times 6?
10
Incorrect.
What is 3 times 6?
18
Correct!
Thank you for playing!
You scored 2 out of 3
<<<<<<<<<< Finished successfully <<<<<<<<<<"

So great, I've managed to do everything except generate a score (yay you got 2 out of 3 or whatever) and I've been staring at it all day. I've used "correct" as a variable because I suppose I'll be able to find a way to generate a score more easily that way. Anything else that seems unnecessarily cumbersome is just me being new and stupid. Here's what I've done:

puts()
puts()
puts("Welcome to Super Multiplier!")
puts("===================================")
puts()
i = 0
while i < 3
puts()
	i = i + 1
x = (rand(10))
y = (rand(10))
z = x * y
puts(("What is ") + String(x) + " times " + String(y) + ("?"))
answer = Integer(gets())
correct = "Correct!"                                  
incorrect = "Incorrect."
if answer == z
	puts(correct)
else
	puts(incorrect)
end
end
puts()
puts("Thank you for playing!")
puts("You scored ??? out of " + String(i) + ".")
puts()

Can someone please put me out of my misery? I want to get on with other exercises.

Thanks in advance!


#2

create a new variable score, for example under the declaration of i, and set it to zero:

i = 0
score = 0

then, if the user gets it correct simple updated the score variable:

if answer == z
	puts(correct)
        score = score + 1

and the put that in the you scored string, but i trust you can do this? I didn't test the code, but it should work, do you get what i am doing?


#3

Yes! Of course! Thank you @stetim94, you are a BOSS.

The simplest things often elude me.


#4

And sorry for not posting in Corner Bar. Should've lurked moar.

End result:

puts()
puts()
puts("Welcome to Super Multiplier!")
puts("===================================")
puts()
i = 0
score = 0
while i < 3
puts()
	i = i + 1
x = (rand(10))
y = (rand(10))
z = x * y
puts(("What is ") + String(x) + " times " + String(y) + ("?"))
answer = Integer(gets())
correct = "Correct!"                                  
incorrect = "Incorrect."
if answer == z
	puts(correct)
	score = score + 1
else
	puts(incorrect)
end
end
puts()
puts("Thank you for playing!")
puts("You scored " + String(score) + " out of " + String(i) + ".")
puts()