Romani ite domum!

NUMERALS = { 'i' => 1, 'v' => 5, 'x' => 10, 'l' => 50, 'c' => 100, 'd' => 500, 'm' => 1000 } def roman_to_integer puts 'Please type a roman numeral: ' roman = gets.chomp.downcase roman_chars = roman.downcase.split('') roman_length = roman_chars.length - 1 integer = 0 while roman_length >= 0 i = NUMERALS[roman_chars[roman_length]] if i integer += i roman_length -= 1 else puts 'Input is not a valid Roman numeral. Please try again.' roman_to_integer end end puts 'As an integer, this is: ' puts integer end roman_to_integer
Run this code.

When you enter a valid Roman numeral, it prints the corresponding integer and stops.
When you enter an invalid one, it will tell you that it's invalid and prompt another input. When finishing that input, it will tell you the result, but it will also tell you again that it was invalid and prompt another input, even if you entered a correct Roman numeral.

Why is that? And how can you fix it?

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.