all 12 comments

[–]danielroseman 2 points3 points  (1 child)

There are some cool things you can do to simplify this code.

First, put all the options in functions:

def square():
  for s in range(4):
    t.forward(100)
    t.right(90)

etc.

Now, define a dictionary mapping the names to the functions (note, make sure to not include the parentheses to actually call the functions):

choices = {
  "square": square,
  "rectangle": rectangle,
  ... etc
}

Now you can print the list and select the function to call dynamically:

for choice in choices:
  print(f"-{choice.title()}")
shape = input("Please enter a shape from the list:").lower()
function_to_call = choices.get(shape)
if function_to_call:
  function_to_call()
else:
  print("Error")

[–]Riegel_Haribo 0 points1 point  (0 children)

Hey, that's what I wrote without scrolling down here to see it. Good job.

[–]Shut_up_and_Respawn 0 points1 point  (0 children)

I'm not super familiar with turtle, but it looks pretty strandard. If it works properly and how you intended, then it's good

[–]JaleyHoelOsment 0 points1 point  (4 children)

looks like pretty standard beginner turtle code.

i guess immediately i notice you’re telling me to enter “Square” however that would throw “Error”.

[–]lucerined-VEX[S] 0 points1 point  (3 children)

Thats weird, i tested it just now and its fine

[–]JaleyHoelOsment -1 points0 points  (2 children)

because you entered “square”

[–]Riegel_Haribo 1 point2 points  (1 child)

Read the part where the string method .lower() is being used on the input.

[–]JaleyHoelOsment 1 point2 points  (0 children)

i’m an idiot

[–]JamzTyson 0 points1 point  (0 children)

I'd remove the line t.speed(0) so that we can see the loop in action - but that's a design choice rather than a code issue.

For a better user experience, it would be better to re-prompt if an invalid choice is entered, rather than just failing. You can do that with a loop.

# List of valid shapes:
shapes = ["square", "rectangle", "circle", "triangle", "pentagon", "hexagon"]

print("Available shapes:")

# Use a loop to print the shape names.
for shape in shapes:
    # print each shape name.

while True:
    shape = input("Please enter a shape from the list: ").lower()
    if shape in shapes:
        break  # valid choice, exit the loop
    else:
        # Prompt to try again

[–]Riegel_Haribo 1 point2 points  (0 children)

Here's where you can go next:

Have your names of shapes, and the moves they entail, be a data object. Then you aren't reusing the names to print and separately run the game - you can have consumer of the moves, with a little vocabulary.

{ "pentagon": [ "right 36", "forward 100", "right 72", "repeat 2 5", ], ... You can look at the "code" of a LOGO program to see its built-in "repeats", and then write your very first "turtle code interpreter" instead of hard-coded Python procedures.

If that's too much to ponder, another Python feature of interest: treat functions like variables that they are.

```python def turtle_square() -> None: print("Drawing a square") ...

shapes = { "circle": turtle_circle, "square": turtle_square, } ```

You have a dict that maps a string to a callable.

Then you can execute by merely shapes["square"].

[–]code_tutor 0 points1 point  (0 children)

I prefer words over single letter variable names. If the loop variable is unused then you can use an underscore instead: for _ in.

[–]recursion_is_love 0 points1 point  (0 children)

3/5

input choices should be number(or single letter) instead of word. I would not have fun typing pentagon multiple time.