There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply () below.
If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.
Join the Discussion. Help a fellow learner on their journey.
Ask or answer a question about this exercise by clicking reply () below!
Agree with a comment or answer? Like () to up-vote the contribution!
To check that I have gotten the necessary concepts, I wrote this: still attempting a (hypothetical) test-score evaluation widget.
Obviously, I’ve done something wrong because it doesn’t run— but I can’t see what I did.
Any corrections much appreciated, thanks!
——————-
print "Enter test score, please: "
test_score = gets.chomp
if test_score >= 90
puts “Well done! Your grade is A.”
elsif 80 >= test_score < 90
puts “Well done! Your grade
is B.”
elsif 70 >= test_score < 80
puts “Very good! Your grade is C.”
else
puts “You have not passed the test. Please retake.”
end
—————-
Unless you click the prompt, type and enter a value within seconds, it will time out and not take any inputs. This is an old problem that cropped up when support for Ruby was dropped about five or more years ago.
If you just want to see the code run, set test_score manually by assignment.
Unnecessary and possibly not even valid.
Ditto with above.
Aside
Assuming the above syntax is valid (as I suspect) it makes better sense to keep the comparisons in the same direction.
Oh, I see what the problem is. I was testing to see if I understood and could figure out how to do that.
The reason I am fiddling with this is that I teach adult learners in a vocational English program. Sometimes I might give longer tests that aren’t just 100 points— so a widget where they could input however many points they got and have it tell them what that is equivalent to might be useful at some point.
Thank you for the feedback— much appreciated!
—>. Why did they stop supporting the Ruby course? That’s really sad— there isn’t anyone else I can ask for assistance. I do hope I am not being a complete nuisance to you.
Second try: It doesn’t prompt me to enter anything, so there’s no way to enter a score… Clearly I have forgotten something.
——————
print "What is your test score? "
test_score = gets.chomp
if test_score >= 90
puts “Well done! Your grade is A.”
if 89 >= test_score >= 80
puts “Well done! Your grade
is B.”
if 79 >= test_score >= 70
puts “Very good! Your grade is C.”
else
puts “You have not passed the test. Please retake.”
The error message says: (ruby) :14: syntax error, unexpected end-of-input, expected keyword_end.
————————
Thank you again, and I am sorry to bother you.
Cannot say, but I suspect it was because there were no authors to keep it up and not enough interest being shown by users. It’s a numbers game when it comes to deciding what products to promote. Python 3 was a product they could sell (in a Pro subscription). Just guessing, though.
Every IF statement (expression?) requires an END.
As for the code it will have problems with floats between 79 and 80, and between 89 and 90. This problem is one I would deal with from the bottom up.
x = 69.5
if x < 70
puts 'retake'
elsif x < 80
puts 'C'
elsif x < 90
puts 'B'
else
puts 'A'
end
Now you can see more what I was getting at in terms of simplified code. When code looks complicated, look at in reverse and see if that doesn’t uncomplicate things.
Very much so, which is a good reason to create it: A defined purpose is self-evident.
For any range of points we can still distribute it over a percentile scale so it’s just a matter of implementing a ratio, applying it to the value (grade) then rank it.
The math is simple, so it just comes down to the program variables. In thinking this through one should impose constraints on how many parameters, data structures and running variables. You’ll find Ruby is exceptionally intuitive along these lines. Everything is a method.
def conversion_rate
1.0 # arbitrary base rate
end
def conversion_rate(m)
m * conversion_rate
end
Get your head wrapped around that idea and Ruby will be a nice fit for you. It may not be popular, but it is still an amazing language and to Rubyists the only language they care to use.
Bottom line, this is a language that one must devote themselves to, as in have a serious goal in mind. It has so much built horsepower it is almost imposing and unwieldly; that is, until you get it in your bones. After that marriage you will be a dedicated stalwart. It won’t be tomorrow, or next month, but it will be sometime in the near future if you truly continue to pursue the language. I didn’t, but that is because I had no reason, but it didn’t stop me getting my feet wet, at least.
Aha— got it! I figured out how to get it to do what I wanted:
print "Thank you for completing this online test. What is your test score? "
score = gets.chomp.to_i
if score >= 90
puts “Well done! Your grade is A.”
elsif score >= 80
puts “Well done! Your grade is B.”
elsif score >= 70
puts “Very good! Your grade is C.”
else
puts “You have not passed the test. Please notify your instructor to retake.”
end
And it works, yay!
(I modeled it on this example I found online:)
print “Welcome to XYZ Museum Ticketing. What is your age?”
age = gets.chomp.to_i
if age >= 62
print “Senior Fare $7.50.”
elsif age >= 14
print “Adult Fare $12.00.”
elsif age >= 4
print “Child Fare $5.00.”
else
print “Admission Free”
end
—————
Thanks!