# ChatGPT for CodeEval – An Experiment

The idea was to see whether we could use ChatGPT to evaluate the code. I tried with two small fragments (one from a tutorial and another I use as an example in an introductory course).  Here are the results.

It did a pretty decent job.

Here is the session with ChatGPT.

Can you evaluate this piece of Python code?
Sure! Please provide the code that you’d like me to evaluate.
Me
# Tutorial Examples # https://pygame-zero.readthedocs.io/en/stable/introduction.html#creating-a-window WIDTH = 300 HEIGHT = 300 def draw(): screen.fill((128,0,0,0))
ChatGPT:
This code defines two variables, WIDTH and HEIGHT, with values 300 each. It also has a function called `draw` that takes no parameters.

The function `draw` appears to fill the screen with a color represented by the RGBA value (128, 0, 0, 0). RGBA stands for Red, Green, Blue, and Alpha, where each value ranges from 0 to 255 and determines the intensity of the respective color channel. The RGBA value (128, 0, 0, 0) results in a solid red color.

It’s important to note that this code likely only makes sense in the context of a larger program that uses the Pygame Zero library. The function `screen.fill` is a Pygame Zero function for filling the screen with a color, and the variable `screen` is likely a Pygame Zero object that represents the game screen.

Can you evaluate the following pythno code and show how we can improve it?
Of course! Please provide the code that you’d like me to evaluate and improve.
Me
“”” random_lines.py – draw random lines with different co-ordinates and colors You will learn: randint function (how to generate random numbers) how to use random numbers to draw lines color functions in turtle penup(), pendown(), goto() functions “”” from random import randint from turtle import * def moveto(x, y): penup() goto(x, y) pendown() for i in range(20): x = randint(-100, 100) y = randint(-100, 100) moveto(x, y) color(“red”) rt(randint(0, 360)) fd(randint(50, 150))
ChatGPT
This code uses the Turtle library to draw random lines with different co-ordinates and colors. It generates 20 random lines using a `for` loop.

There are a few things that can be improved in this code:

1. Naming conventions: The function name `moveto` does not follow the PEP 8 style guide’s recommendations for function names. In PEP 8, function names should be in lowercase, with words separated by underscores. A better name for this function would be `move_to`.
2. Function reuse: The code for moving the turtle pen to a specific position is repeated for each iteration of the loop. This code can be extracted into a separate function and reused, improving code maintainability and reducing the size of the main function.
3. Modularization: The code could be broken down into multiple functions, each with a specific purpose. This would make the code more modular and easier to understand.

Here is an improved version of the code that incorporates these changes:

scss
```import random from turtle import *```def move_to(x, y):
penup()
goto(x, y)
pendown()def draw_line(x, y):
move_to(x, y)
color(“red”)
rt(random.randint(0, 360))
fd(random.randint(50, 150))def draw_random_lines():
for i in range(20):
x = random.randint(-100, 100)
y = random.randint(-100, 100)
draw_line(x, y)if __name__ == “__main__”:
draw_random_lines()
done()

Me:

Can you improve it?

This code uses the turtle and random modules to draw 20 random lines on the screen. The `moveto` function is defined to move the turtle’s pen to a specified (x, y) position without drawing a line.

Some ways to improve the code are:

2. Use PEP 8 guidelines to format the code, making it easier to read and follow. For example, function and variable names should be in snake case (e.g. `move_to` instead of `moveto`).
3. Make the code more modular and easier to reuse by encapsulating the logic for drawing one random line into its own function.
4. Add error handling to prevent the program from crashing if the random numbers generated result in the turtle going off the screen.

Here’s an example of the improved code:

python
```import random import turtle```def move_to(x, y):
“””Move the turtle’s pen to the specified (x, y) position without drawing a line.”””
turtle.penup()
turtle.goto(x, y)
turtle.pendown()def draw_random_line():
“””Draw a random line with a random color.”””
x = random.randint(-100, 100)
y = random.randint(-100, 100)
move_to(x, y)
turtle.color(“red”)
turtle.right(random.randint(0, 360))
turtle.forward(random.randint(50, 150))def main():
“””Draw 20 random lines on the screen.”””
turtle.speed(“fastest”)
for _ in range(20):
draw_random_line()
turtle.done()

if __name__ == “__main__”:
main()

ChatGPT Jan 30 Version. Free Research