r/learnruby • u/955559 • Sep 30 '16
Temperature Converter
someone suggested to try rewriting your scripts in another launguage, so here is a port of my python temperature converter
puts "This program converts the tempurature between celsius, kelvin, and
fahrenheit scales,enter the temperature followed by the first letter of the
scale, e.g. 32c for 32 celsius \n"
input = gets.chomp
scale = input.slice(-1)
digitslength = input.length - 1
digitstring = input.slice(0,digitslength)
digits = Integer(digitstring)
if scale == 'c'
fahrenheit = digits * 1.8 + 32
kelvin = digits + 273.15
print "Fahrenheit =", fahrenheit, "\n"
print "Kelvin =", kelvin
elsif scale == 'f'
celsius = (digits - 32) / 1.8
kelvin = (digits + 459.67) * 0.55555556
print "Celsius =", celsius, "\n"
print "Kelvin =", kelvin
elsif scale == 'k'
celsius = digits - 273.15
fahrenheit = digits / 0.55555556 - 459.67
print "Celsius =", celsius, "\n"
print "Fahrenheit =", fahrenheit
end
2
Upvotes
View all comments
Show parent comment
1
u/955559 Oct 07 '16
since learned not to put the print statements in if/elif blocks, as its not DRY, too lazy to rewrite it in python or ruby, but heres a c example showing the idea