Search code examples
if-statementbasic

Pointers to make this small basic code work


I am beginning with small basic and am having a somewhat bad time dealing with if, else, then, and elseif statements. I have this code that is supposed to draw different shapes with turtle when the user inputs the first letter of the shape. I already this type of code with a streetlight but I need to head in the right direction with this new code. I have all the code for the shapes written, but every time I input the letter for the shape it just draws the first shape which is the triangle. At one point I had the code set up differently where all the shapes would get get drawn right next to each other. I think i'm on the right track.

'what does the code do? 'makes user decide what shape they want and turtle will draw it

T= "Triangle"
P= "Parallelogram"
S= "Square"


TextWindow.ForegroundColor= "magenta"
TextWindow.WriteLine("What shape do you want to draw?")
TextWindow.Write (" Choose [T] for triangle, [P] for Parallelogram or [S] for Square : ")
shape=textwindow.Read()
If T = "Triangle" Then
  Turtle.Speed=9
  GraphicsWindow.Width = 500  
  GraphicsWindow.Height=500
  Turtle.PenUp()
  Turtle.Move(200)
  Turtle.TurnRight()
  Turtle.Move(145)
  Turtle.TurnRight()
  Turtle.PenDown()
  Turtle.Move(320)
  Turtle.TurnRight()
  Turtle.Move(320)
  Turtle.Turn(135)
  Turtle.Move(451)
  Turtle.Hide()
elseif P = "Parallelogram" Then
  Turtle.Speed=9
  GraphicsWindow.Width = 500  
  GraphicsWindow.Height=500
  Turtle.TurnLeft()
  Turtle.PenDown()
  Turtle.Move(200)
  Turtle.Turn(120)
  Turtle.Move(120)
  Turtle.Turn(60)
  Turtle.Move(200)
  Turtle.Turn(120)
  Turtle.Move(120)
Elseif S = "Square" then
  Turtle.Speed=9
  Turtle.TurnLeft()
  Turtle.PenDown()
  Turtle.Move(200)
  Turtle.TurnRight()
  Turtle.Move(200)
  Turtle.TurnRight()
  Turtle.Move(200)
  Turtle.TurnRight()
  Turtle.Move(200)
EndIf

Solution

  • Look at the test in your if statements. The first one, for example tests a variable called T (which has a value "Triangle") against the string "Triangle". This will always be true, so it will always draw a triangle, and not get any further. What do you really want to compare to the string "Triangle"? The variable you have just received from the user... not the variables T, P and S.

    P.S. If possible, get into the habit of using consistent indentation, it will help you see the structure of your code more easily. All the best!