Basic If/Else statement question - ruby

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
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"

Related

Invalid Syntax Python

def solution(t, p):
extracted_number = []
answer = 0
while(len(t) >= len(p)):
extracted_number.append(t[:len(p)])
t = t[1:]
for x in extracted_number:
if(int(x) <= int(p)):
answer +=1
return answer
I want to make the code concise, so I did this.
answer +=1 for x in extracted_number if (int(x) < int(p)) --> invalid syntax
How to revise?
I think you can try out answer=answer+1 and then u can return that answer variable

True being input to function

When I run this program
def tng(x)
tn =( x * (x+1) )/2
return tn
end
i = 0
while tng (i) <= 500
i += 1
end
puts i
it gives me the error "undefined method `+' for true:TrueClass (NoMethodError)". I suppose this means that true is input to the tng(x) function, but why would this occur?
In Ruby spaces before parentheses are significant. Parser treats
while tng (i) <= 500
as:
while tng((i) <= 500)
the latter is evaluated to true hence the error.
Sidenote: do not use return as a last statement in methods, it’s returned automatically. Also, don’t use while loops, use iterators [unless you do perfectly understand why a generic loop is to be used here]:
def tng(x)
(x * (x + 1)) / 2
end
1.upto(Float::INFINITY).each do |i|
break i unless tng(i) <= 500
end

How to nest while loops in Ruby

I'm trying to make a simple * pyramid using while loops but it stops at the first five *. I can't figure out why.
This is my code:
x = 5
y = 0
while x > 0
while y < x
print "*"
y +=1
end
x -= 1
end
You never reset y or print a new line
x = 5
y = 0
while x > 0
while y < x
print "*"
y +=1
end
print "\n"
y = 0
x -= 1
end
Output
*****
****
***
**
*
That is bad Ruby tho
This is a much more idiomatic solution
5.downto(1) do |x|
1.upto(x) do |y|
print "*"
end
print "\n"
end
Output
*****
****
***
**
*
I don't know what the final shape of "pyramid" you're looking for, but you can likely adapt the technique above to get the desired output

syntax error, unexpected keyword_end end [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I'm getting into Ruby and I've searched and searched and looked at syntax, yet I can't figure out what's going wrong.
The program is supposed to solve a ProjectEuler problem.
I'm encountering four of this syntax error:
unexpected keyword_end end
This is my code:
grid = #20x20 "grid"/array of numbers
largest = 0
lateral(0,1,2,3)
vertical(0,20,40,60)
diagonal_right(0,21,42,63)
diagonal_left(3,22,41,60)
puts largest
#lateral
def lateral(a, b, c, d)
while (d < grid.size)
temp = grid[a] * grid[b] * grid[c] * grid[d]
if (temp > largest)
largest = temp
end
if ((d % 19) == 0)
a += 4
b += 4
c += 4
d += 4
else
a++
b++
c++
d++
end # <===== getting syntax error here
end
end
def vertical(a, b, c, d)
while (d < grid.size)
temp = grid[a] * grid[b] * grid[c] * grid[d]
if (temp > largest)
largest = temp
end
a++
b++
c++
d++
end # <===== getting syntax error here
end
def diagonal_right(a, b, c, d)
while (d < grid.size)
temp = grid[a] * grid[b] * grid[c] * grid[d]
if (temp > largest)
largest = temp
end
if ((d % 19) == 0)
a += 4
b += 4
c += 4
d += 4
else
a++
b++
c++
d++
end # <===== getting syntax error here
end
end
def diagonal_left(a, b, c, d)
while (d < (grid.size - 4))
temp = grid[a] * grid[b] * grid[c] * grid[d]
if (temp > largest)
largest = temp
end
if ((a % 19) == 0)
a += 4
b += 4
c += 4
d += 4
else
a++
b++
c++
d++
end # <===== getting syntax error here
end
end
I marked the four spots where I'm getting the syntax errors.
I've adjusted parentheses, played with and double-checked the ends positions and amount. I do not understand what's wrong with it. Could it be an interpreter problem? I'm using a MacBook Pro.
The ++ operator doesn't exist in Ruby. You should go for += 1.
You have very strange formatting. If you had formatted your code properly by following one of the many Ruby styleguides (or simply hitting Alt+Shift+F or whatever the combination is in your editor of choice), you would have immediately seen the problem. According to most style guides, there should be a space to both sides of a binary infix operator and no space after a unary prefix operator. I.e. you should write foo - bar and !baz. You have no space around your infix operators and a newline in between the unary prefix operator and its operand.
This is what it looks like correctly formatted:
a + +b + +c + +d + + # plus what?
Do you see the problem? You are missing the operand to the last unary prefix + operator.

Syntax error in simple program

I'm new to programming and I'm trying to program something but there's some kind of syntax error which I can't work out. Any help would be much appreciated. Here's my code:
begin
puts"Enter a number to count, or to exit type 0."
y = gets.chomp.to_i
if y == 0
exit
end
puts"Now put the number you're starting with"
x = gets.chomp.to_i
if y + x == 12 or y + x < 12
print x + y
end
if y + x > 12
n = y + x - 12
end
begin
if n < 12 or n == 12
print n
end
if n > 12
n = n - 12
end
end until if n < 12 or n == 12
end until y == 0
end
Your use of until if if wrong. They are each control sequences. You shouldn't need both.
Your n is not visible later in the code. Declare n=0 for example before if y + x > 12 to make it visible and accessible in the relevant code blocks.
Then, until if is wrong, this should simply be until
Lastly, delete the last end keyword.

Resources