Home > database >  How to make User input choose shape of turtle
How to make User input choose shape of turtle

Time:01-27

import turtle


player = input('Pick Circle or Arrow')
if player == Arrow:
    turtle.Turtle()
    turtle.shape(Arrow)
elif player == Circle:
    turtle.Turtle()
    turtle.shape(Circle)
else:
    print ('Choose Circle or Arrow')

sorry if this is a stupid question, I am new to coding. This is for a maze game where the user can pick the shape of the turtle, I've tried a lot of stuff but it doesn't work, if you can help it would be great thanks.

CodePudding user response:

The return value of input() is a string, so you need to compare against it with a string. Also, "arrow" and "circle" need to be in their lowercase forms for turtle because they don't do that conversion automatically. You also need to store a reference to the created turtle, and it's easier to just do that once instead of it being in each path.

import turtle

player = input('Pick Turtle or Arrow')
screen = turtle.Screen()
t = turtle.Turtle()
if player == "Arrow":
    t.shape("arrow")
elif player == "Circle":
    t.shape("circle")
else:
    print ('Choose Turtle or Arrow')
    screen.bye()

if you don't have the quotation marks around Arrow and Circle, python thinks that they are variable names.

CodePudding user response:

I see several problems with your code and @Samathingamajig's recommended changes: you don't keep a handle on the turtle you create, so it's useless to you; you use uppercase for the turtle shapes even though only lowercase is recognized; you really don't leave yourself any logic to go back and let the user correct their choice after detecting it's no good.

Here's how I might approach this problem:

from turtle import Screen, Turtle

SHAPES = {
    'Circle': 'circle',
    'Arrow': 'arrow',
    'Yertle': 'turtle',
}

shape = ""

while shape not in SHAPES:

    shape = input("Pick "   ' or '.join(SHAPES)   ": ")

screen = Screen()

turtle = Turtle()

turtle.shape(SHAPES[shape])
turtle.color('red')
turtle.forward(100)

# ...

screen.exitonclick()
  • Related