Basic If/Else statement question
I'm getting a strange error: "syntax error, unexpected $end, expecting kEND" and it points to the final line of my code.
New to Ruby and not sure what I'm doing wrong here. Any help would be fantastic. Thanks!
def add(x,y)
if(x > y)
c = x + y
return c开发者_如何学Python
else
puts "Y is too big"
return
end
a = 4
b = 6
add(a,b)
BTW, you can refactor your if..end statement out completely if you prefer
def add(x,y)
return (x + y) if(x > y)
puts "Y is too big"
end
Corrected code (you are missing one end for the if-else):
def add(x,y)
if(x > y)
c = x + y
return c
else
puts "Y is too big"
return
end
end
a = 4
b = 6
add(a,b)
Both if
statements and function definitions require an end
statement to terminate them.
Try adding another end
after your existing end
and your problem should go away.
wap to input principle, rate, time and choice. if choice is 1 than calculate simple interest, if choice is 2 calculate compund interest and if it is other than 1 or 2 print "Enter valid choice"
精彩评论