Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Help with Python Turtle: The code is refusing to run with weird errors, and need code to run flawlessly. Receiving errors as code being undefined

Help with Python Turtle: The code is refusing to run with weird errors, and need code to run flawlessly.
Receiving errors as code being undefined such as (examples):
NameError: name 'lake' is not defined. + Traceback error.
NameError: name 'ocean' is not defined.
My code so far:
import turtle
import random
from tkinter import Tk, Label, Entry, Button, Scale
# Define global variables to store user inputs
num_lakes =0
num_mountains =0
num_rivers =0
size_multiplier =1.0
def get_user_input():
"""
Creates a GUI using Tkinter to obtain user input for the number of lakes, mountains, and rivers.
"""
def handle_close():
global num_lakes, num_mountains, num_rivers, size_multiplier
num_lakes = int(lake_entry.get()) if lake_entry.get() else 0
num_mountains = int(mountain_entry.get()) if mountain_entry.get() else 0
num_rivers = int(river_entry.get()) if river_entry.get() else 0
size_multiplier = size_scale.get()
window.destroy()
window = Tk()
window.title("Island Generator Settings")
lake_label = Label(window, text="Number of Lakes:")
lake_label.grid(row=0, column=0)
lake_entry = Entry(window)
lake_entry.grid(row=0, column=1)
mountain_label = Label(window, text="Number of Mountains:")
mountain_label.grid(row=1, column=0)
mountain_entry = Entry(window)
mountain_entry.grid(row=1, column=1)
river_label = Label(window, text="Number of Rivers:")
river_label.grid(row=2, column=0)
river_entry = Entry(window)
river_entry.grid(row=2, column=1)
size_scale_label = Label(window, text="Island Size (Scale)")
size_scale_label.grid(row=3, column=0)
size_scale = Scale(window, from_=1, to_=3, orient='horizontal', resolution=0.1)
size_scale.set(1) # Default size multiplier of 1
size_scale.grid(row=3, column=1, columnspan=2)
button = Button(window, text="Generate Island", command=handle_close)
button.grid(row=4, column=0, columnspan=3)
window.mainloop()
# Call the function to get user input
get_user_input()
# Set up the turtle screen
screen = turtle.Screen()
screen.setup(width=800, height=600)
screen.bgcolor(ocean)
# Create a turtle instance
my_turtle = turtle.Turtle()
my_turtle.speed(0)
def draw_circle(radius, line_color, fill_color):
"""
Draws a circle with the specified radius, line color, and fill color.
Args:
radius (int): The radius of the circle.
line_color (str): The color of the circle's outline.
fill_color (str): The color to fill the circle.
"""
my_turtle.color(line_color)
my_turtle.fillcolor(fill_color)
my_turtle.begin_fill()
my_turtle.circle(radius)
my_turtle.end_fill()
def move_turtle(x, y):
"""
Moves the turtle to the specified coordinates without drawing a line.
Args:
x (int): The x-coordinate of the destination.
y (int): The y-coordinate of the destination.
"""
my_turtle.penup()
my_turtle.goto(x, y)
my_turtle.pendown()
def draw_lake():
"""
Draws a random lake circle within the boundaries of the landmass.
Increases the size of the lake based on the size multiplier.
"""
x_limit = int(150* size_multiplier)//2-30* size_multiplier # Adjusted for lake size
y_limit = int(120* size_multiplier)//2-30* size_multiplier # Adjusted for lake size
x = random.randint(-x_limit, x_limit)
y = random.randint(-y_limit, y_limit)
move_turtle(x, y)
radius = random.randint(10,30)* size_multiplier
draw_circle(radius, lake, lake)
# Draw lakes
for _ in range(num_lakes):
draw_lake()
# More functions to draw mountains and rivers can be added here using similar patterns
# Hide the turtle and display the result
my_turtle.hideturtle()
turtle.done()

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

SQL Instant Reference

Authors: Gruber, Martin Gruber

2nd Edition

0782125395, 9780782125399

More Books

Students also viewed these Databases questions

Question

=+Does it make you feel cool?

Answered: 1 week ago