Syntax Error on Lesson 6


#1



print "Need a dispenser-, err I need some input here!"

user_input = gets.chomp 
user_input.downcase!

print "You got an S there, son." if user_input.gsub!(/s/, "th")
else print "A spy's sappin' my sentry! And he doesn't have an S!"
end

So, yeah. Hi there. I've been having problems with creating the Else branch. It keeps coming up with :

(ruby):6: syntax error, unexpected keyword_else, expecting $end
else print "A spy's sappin' my sentry! And he doesn't have an S!"
^ : every time I try to submit it. And I have tried other variations of this and looked on the other threads for guidance, and my code SEEMS to be fine. Like, am I missing something? Did I delete something without knowing? What's going on here? Please help?


#2

Ruby is saying that else isn't allowed there, so you can then make a pretty strong guess that you're using if/else wrong so then you can google for how to use that


#3

It's REALLY strange though, because one time I submitted it WITHOUT the Else statement and it worked how it was supposed to! Like, that utterly confused me. (of course, it didn't count it because I didn't have an else statement, but it worked correctly.) I'll try changing the layout up a bit and see how that plays out. But, is there any tips you could give me with using the else/if statements?

I've been looking on the glossary for examples of it's use. Here's the updated code i've been trying to get working following the glossary's example for Else.

print "Need a dispenser-, err I need some input here!"

user_input = gets.chomp 
user_input.downcase!

if 
  user_input.gsub!(/s/, "th") print "You got an S there, son." 
else
  print "A spy's sappin' my sentry! And he doesn't have an S!"
end

Been trying that and it still says invalid syntax. Am I missing something obvious?


#4

It also gives suggestions/an explanation, you say that you got a syntax error, but not that you've considered what else it was saying with that syntax error, so that's missing something obvious, yes.

aou.rb:7: syntax error, unexpected tIDENTIFIER, expecting keyword_then or ';' or '\n'
  user_input.gsub!(/s/, "th") print "You got an S there, son."
                                   ^
aou.rb:8: syntax error, unexpected keyword_else, expecting end-of-input

That might be more informative than your syntax error message, not sure what you got. (later ruby version)

You can also do more to bring your code closer to examples of if/else that you find by googling. You should be able to study the examples and apply the same pattern to your code/identify where you don't follow it. A more fool-proof approach is to copy the working example and change out one small part at a time until it looks like your code, running the code after each change to ensure it's still valid


#5

I've been trying different keywords and other things with my code to no avail. I'm putting the syntax error here of my original to see if I can see a similarity to yours and mine to hopefully tell what's wrong better.

(ruby):6: syntax error, unexpected tIDENTIFIER, expecting keyword_then or ';' or '\n'
  user_input.gsub!(/s/, "th") print "You got an S there, son." 
                                   ^
(ruby):7: syntax error, unexpected keyword_else, expecting $end

#6

That's the same.

Note that it points at a location with a ^

At that location, it says: unexpected tIDENTIFIER
That's refering to print

And then it follows up by saying what it expected based on what it had read so far: ; or \n (newline) or then

So then you can either say, yeah it should be one of those there first, or, you could say, none of that is what I meant, and then you'd have to change what comes before

Also, it's important to agree with the change, don't take the suggestion without understanding it. That may mean reading up on if/else some more and paying some extra attention to mentions of those suggested things (because ruby doesn't know what you mean, so it can't tell you what to type)


#7

Oh.... OH. I didn't know that arrow pointed to the location of the error, nor did I know "then" (nor these symbols \n, ; ) was /were thing(s) I could put down to edit my code. All I had to do was put "then" between the gsub! and print statements. Literally everything else was correct, I just needed another keyword.

I feel incredibly dumb for not understanding what the code was trying to say. But, thanks for the help. Heaven knows I needed it. xD

(it works now by the way.)


#8

Usually you would write an if-expression like this:

if CONDITION
    stuff
end

Note that there is a newline (\n) after the condition, this could also be a then instead:

if CONDITION then stuff end

or both

if CONDITION then
    stuff
end

There's also another form that goes like:

stuff if CONDITION

This form doesn't have an optional else or elsif


#9

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.