user2369480 -4 years ago 65
Ruby Question

# Head First Ruby - #{10 — num_guesses} error

Here is book code. And he is not work...

``````# Get My Number Game
# Written by: you!
puts "Welcome to 'Get My Number!'"
# Получение имени игрока и вывод приветствия.
input = gets
name = input.chomp
puts "Welcome, #{name}!"
# Сохранение случайного числа.
puts "I've got a random number between 1 and 100."
puts "Can you guess it?"
target = rand(100) + 1
# Отслеживание количества попыток.
num_guesses = 0
# Признак продолжения игры.
guessed_it = false
until num_guesses == 10 || guessed_it
puts "You've got #{10 — num_guesses} guesses left."
print "Make a guess: "
guess = gets.to_i
num_guesses += 1
# Сравнение введенного числа с загаданным
# и вывод соответствующего сообщения.
if guess < target
puts "Oops. Your guess was LOW."
elsif guess > target
puts "Oops. Your guess was HIGH."
elsif guess == target
puts "Good job, #{name}!"
puts "You guessed my number in #{num_guesses} guesses!"
guessed_it = true
end
end
# Если попыток не осталось, сообщить загаданное число.
unless guessed_it
puts "Sorry. You didn't get my number. (It was #{target}.)"
end
``````

The error:

``````test.rb:18: syntax error, unexpected tIDENTIFIER, expecting t│ => String
STRING_DEND                                                  │2.4.0 :016 > name * 3
puts "You've got #{10 — num_guesses} guesses left."
``````

I'm not sure what that character is between the `10` and the `num_guesses` but it's probably a hyphen.

``````#{10 — num_guesses}
``````

it's not a minus sign, which would look like this...

``````#{10 - num_guesses}
``````

What sort of editor did you use to enter your code?

Anyway, change the hyphen to a minus sign and that will fix your problem.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download