2013-03-07 76 views
1

我刚开始学习Ruby。我正在创建一个简单的程序,但我仍然不确定如何使用它。这是我的代码:

=begin 
Filename: GameCompanyPoem.rb 
Program Name: Poem Generator 
Version 1.0 
Created: March 7th, 2013 
Purpose: To generate a poem based on key words given by the user 
=end 
print 'The following program will ask you to input words. Based on the words you input it will generate a poem. Now, please enter a name. ' 
name = gets.chomp 
print 'Please enter a mammal. ' 
mammal = gets.chomp 
print 'Now please enter a colour. ' 
colour1 = gets.chomp 
print 'We are almost done! Please enter a place or location now. ' 
place = gets.chomp 
print 'Now enter a verb. ' 
verb1 = gets.chomp 
print 'And another verb. ' 
verb2 = gets.chomp 
print 'Your poem will now be generated. Press enter to continue. 
space = gets.chomp 
print 'Someone by the name of ' + name + 'had a little' + mammal + ', little ' + mammal + ', little ' + mammal + ',' + name + 'had a little ' + mammal + 'its fleece was' + colour + 'as snow. It followed her to ' + place + 'one day, ' + place + 'one day, ' + place + 'one day, it made the children ' + verb1 + 'and ' + verb2 + 'to see a ' + mammal + 'at' + place + '."' 

当我运行它,在最后的方法(打印),每一个空间都有这样的错误:

意外tIDENtifier,期待$结束

在每一个空间,如果我填补它,然后就不见了,但它进入到下一格

+0

闭引号上缺少''你的诗现在将产生。按enter键继续。“语法突出显示器使其更加明显。 – 2013-03-07 18:20:17

+0

并且在最后一个字符串末尾有一个额外的'''(双引号)。请参阅我的回答以获得完整修复 – BlackHatSamurai 2013-03-07 18:21:51

+0

,它应该是coulor1,而不是颜色,就像@limelights提到的。 – BlackHatSamurai 2013-03-07 18:24:05

回答

1

尝试:

print 'Your poem will now be generated. Press enter to continue.' 
print 'Someone by the name of ' + name + 'had a little' + mammal + ', little ' + mammal + ', little ' + mammal + ',' + name + 'had a little ' + mammal + 'its fleece was' + colour1 + 'as snow. It followed her to ' + place + 'one day, ' + place + 'one day, ' + place + 'one day, it made the children ' + verb1 + 'and ' + verb2 + 'to see a ' + mammal + 'at' + place + '.' 
+1

另外,他错过了变量'colour',它应该是'colour1'!:) – 2013-03-07 18:22:40

+0

好的接收!已添加。谢谢! – BlackHatSamurai 2013-03-07 18:23:40

+0

感谢您的帮助!我修正了颜色变量,发现我在某处丢失了一个撇号那里:) – user2145566 2013-03-07 18:27:59

2

一个NIFT y在包括Ruby在内的很多语言中使用技巧插入

print "Someone by the name of #{name} had a little #{mammal}, little #{mammal}, little #{mammal}, had a little #{mammal} its fleece was #{colour} as snow. It followed her to #{place} one day, #{place} one day, #{place} one day, it made the children #{verb1} and #{verb2} to see a #{mammal} at #{place}." 

嗯,它可能不会再押韵了。

还有一点。查看Ruby Interpolation了解详细信息。但基本上ruby运行到#{something},它将其替换为某个结果。

print "1 + 2 = #{1 + 2}"将打印

1 + 2 = 3

相关问题