错误消息一直保持在codecademy上,我不明白为什么,欢迎任何帮助!
这是我的代码:
movies={
Lala:3,
VV:4
}
puts "What to do?"
choice=gets.chomp
case choice
when "add"
puts "What movie you wanna add?"
title=gets.chomp
if movies[title.to_sym].nil?
puts "What rating for the movie?"
rating=gets.chomp
movies[title.to_sym]=rating.to_i
puts "Movie and rating added!"
else
puts "movie already in list..."
end
when "update"
puts "what movie to update?"
title=gets.chomp
if movies[title.to_sym].nil?
puts "Error movie not in list"
else
puts "New rating?"
rating=gets.chomp
movies[title.to_sym]=rating.to_i
puts "Rating updated"
end
when "display"
movies.each do |movies,rating|
puts "#{movies}: #{rating}"
end
when "delete"
puts "Movie to delete?"
title = gets.chomp
if movies[title.to_sym].nil?
puts "Movie not found"
else
movies.delete(title.to_sym)
puts "Movie deleted"
end
以下是错误消息:
(ruby): syntax error, unexpected tIDENTIFIER, expecting $end
... Lala:3, VV:4 } puts "What to do?" choice=gets.c...
...
帮助!请!谢谢!
答案 0 :(得分:1)
您需要在代码中添加一个end
。
movies={
Lala:3,
VV:4
}
puts "What to do?"
choice=gets.chomp
case choice
when "add"
puts "What movie you wanna add?"
title=gets.chomp
if movies[title.to_sym].nil?
puts "What rating for the movie?"
rating=gets.chomp
movies[title.to_sym]=rating.to_i
puts "Movie and rating added!"
else
puts "movie already in list..."
end
when "update"
puts "what movie to update?"
title=gets.chomp
if movies[title.to_sym].nil?
puts "Error movie not in list"
else
puts "New rating?"
rating=gets.chomp
movies[title.to_sym]=rating.to_i
puts "Rating updated"
end
when "display"
movies.each do |movies,rating|
puts "#{movies}: #{rating}"
end
when "delete"
puts "Movie to delete?"
title = gets.chomp
if movies[title.to_sym].nil?
puts "Movie not found"
else
movies.delete(title.to_sym)
puts "Movie deleted"
end
<=# Need an extra `end` here.
您没有结束案例陈述。
答案 1 :(得分:0)
你在这里得到了很多错误的建议,因为显然有些人不熟悉Ruby 1.9样式的哈希声明。 Ruby 1.9或更高版本中的此代码中没有语法错误。如果您使用过时的Ruby 1.8.7,您将收到错误。
由于您的不规则缩进,您似乎错过了end
个错误:
when "delete"
puts "Movie to delete?"
title = gets.chomp
if movies[title.to_sym].nil?
puts "Movie not found"
else
movies.delete(title.to_sym)
puts "Movie deleted"
end # Omitted in original
end
不清楚为什么if
会获得额外的缩进级别,但它显然缺少自己的end
。
除此之外,我还使用Ruby 1.9测试了您的代码,这是一个严重过时的Ruby版本,并且可以正常运行。更新的版本也没有问题,例如最新的2.3.1。
解决这个问题的方法是获得一个不古老的Ruby版本。 Ruby 1.8.7一直延续生命支持直到2013年,但它现在已经死了。许多宝石和应用程序都不再支持Ruby 1.9。
如果可能,请获取当前版本的Ruby并尝试使用您的代码。它应该工作。
注意,这里的一个问题是你的不规则缩进,这使得发现语法错误异常困难。