How can I get the equation I am calculating to show the user the a value * x to the second power. An error pops up when I try to square x

StackOverflow https://stackoverflow.com/questions/21589137

  •  07-10-2022
  •  | 
  •  

سؤال

puts "Let's get started calculating your parabola. What is your A value?"
a = gets.chomp

puts "What is your B value?"
b = gets.chomp
puts "What is your C value?"
c = gets.chomp
x = x
2x = (x.to_i**2)

puts "Your parabola equation is 'y = " + a.to_s + 2x.to_s + " + " + b.to_s + x + " + " + c.to_s + "'. Would you like to go back to the beginning?"
هل كانت مفيدة؟

المحلول

In Ruby variable names can't start with a number, but you did it, 2x = (x.to_i**2). Write it as x2 = (x.to_i**2). Then replace all 2x with x2 in your code.

Another error will be x=x, this is also invalid. Hope you mistyped. Correct it also.

Read this Local Variable Names

A local variable name must start with a lowercase US-ASCII letter or a character with the eight bit set. Typically local variables are US-ASCII compatible since the keys to type them exist on all keyboards.

(Ruby programs must be written in a US-ASCII-compatible character set. In such character sets if the eight bit is set it indicates an extended character. Ruby allows local variables to contain such characters.)

A local variable name may contain letters, numbers, an _ (underscore or low line) or a character with the eighth bit set.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top