What's wrong with this code?

I made a program to motivate me for the last day of NaNoWriMo, but it
keeps getting ‘enemy.rb:7: syntax error, unexpected kELSE, expecting
$end’
Please tell me what’s wrong with the code.
Yes, it’s pretty rough, because I need to make it now, but can someone
please make the code work by telling me what’s wrong with it? I don’t
need it to be elegant or optimized, I just need to use the program.

Here’s the program

puts ‘Enter your goal words for today’
@goal = gets.chomp.to_i
@count = 0
@now = Time.new
@thir = Time.new + (30 * 60)
@en_points = 0
@ur_points = 0
While @count != @goal
if @now == @thir
@num = 500 + rand(300)
puts “#{@num}” + ’ is your enemy word count. How did you fare?’
puts ‘Enter how many words you typed during the thirty miinutes’
@cur = gets.chomp.to_i
@count = @count + @cur
if @cur < @num
puts ‘You lost’
@en_points = @en_points + 1
puts 'Enemy: ’ + “#{@en_points}” + ’ You: ’ + “#{@ur_points}”
elsif @cur == @num
puts ‘A tie’
puts 'Enemy: ’ + “#{@en_points}” + ’ You: ’ + “#{@ur_points}”
else
puts ‘You won’
@ur_points = @ur_points + 1
puts 'Enemy: ’ + “#{@en_points}” + ’ You: ’ + “#{@ur_points}”
end
else
@now = Time.new
end
end
If @en_points > @ur_points
puts ‘You lost despite success.’
elsif @en_points == @ur_points
puts ‘A tie, but you succeeded in writing a lot of words.’
else
puts ‘Multiple victories!’
end

Sorry, if it isn’t good enough. The file is right here.

On Tuesday 29 November 2011 Kenley T. wrote

puts ‘Enter your goal words for today’
puts ‘Enter how many words you typed during the thirty miinutes’
puts ‘You won’
puts ‘A tie, but you succeeded in writing a lot of words.’
else
puts ‘Multiple victories!’
end


Posted via http://www.ruby-forum.com/.

That error means that there’s an else keyword without the corresponding
if. In
your case, this is caused by having an If rather than if (note the
different
capitalization) in the seventh line from the end. Also, you use a While
instead of while.

Stefano

It’s still not working. Still has the same error. Here’s the new code:
puts ‘Enter your goal words for today’
@goal = gets.chomp.to_i
@count = 0
@now = Time.new
@thir = Time.new + (30 * 60)
@en_points = 0
@ur_points = 0
while @count != @goal
if @now == @thir
@num = 500 + rand(300)
puts “#{@num}” + ’ is your enemy word count. How did you fare?’
puts ‘Enter how many words you typed during the thirty miinutes’
@cur = gets.chomp.to_i
@count = @count + @cur
if @cur < @num
puts ‘You lost’
@en_points = @en_points + 1
puts 'Enemy: ’ + “#{@en_points}” + ’ You: ’ + “#{@ur_points}”
elsif @cur == @num
puts ‘A tie’
puts 'Enemy: ’ + “#{@en_points}” + ’ You: ’ + “#{@ur_points}”
else
puts ‘You won’
@ur_points = @ur_points + 1
puts 'Enemy: ’ + “#{@en_points}” + ’ You: ’ + “#{@ur_points}”
end
else
@now = Time.new
end
end

if @en_points > @ur_points
puts ‘You lost despite success.’
elsif @en_points == @ur_points
puts ‘A tie, but you succeeded in writing a lot of words.’
else
puts ‘Multiple victories!’
end

I think you should try running it just to be safe, so the file is
attached.
Thanks!

Why are you using so many instance variables (atvars or @vars)? If you
removed the @'s it might add to the readability of your code. Also, I
believe that you are calculating the time wrong, but I am not quite
sure.
Also, the intent and structure of your code is confusing. Try extracting
some more methods.

If I am not right, please let me know.

On Tue, Nov 29, 2011 at 10:20 AM, Kenley T. [email protected] wrote:

@num = 500 + rand(300)
puts 'Enemy: ’ + “#{@en_points}” + ’ You: ’ + “#{@ur_points}”
if @en_points > @ur_points

Attachments:
http://www.ruby-forum.com/attachment/6803/enemy.rb


Posted via http://www.ruby-forum.com/.


Sincerely,

Isaac S.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs