-1

here I have the problem in line 68 of the code where I evaluate the string of code"snake_y=snake_y-14" how can I evaluate a string with the assignment because I have not seen any questions regarding assignment operations being used in eval() function

import pygame
import random
import time
import sys


def basic_defaults():
    pygame.init()
    snake_x = 200
    snake_y = 175
    font_for_game_title = pygame.font.Font(None, 36)
    screen = pygame.display.set_mode((800, 600)) #Making Screen for the game
    screen.blit(font_for_game_title.render("Snake Game", True, (0, 255, 255)), (300, 10))# Making title appear
    Clock = pygame.time.Clock()
    yolo=""
    return screen, snake_x, snake_y,Clock,yolo


def snake_movements():
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP:
                yolo = "snake_y = snake_y - 14"
                return yolo
            elif event.key == pygame.K_DOWN:
                yolo = "snake_y = snake_y + 14"
                return yolo
            elif event.key == pygame.K_LEFT:
                yolo = "snake_x = snake_x - 14"
                return yolo
            elif event.key == pygame.K_RIGHT:
                yolo = "snake_x = snake_x + 14"
                return yolo
            else:
                #yolo = "snake_x += 14"
                #return yolo
                pass
def game_loop():
    screen, snake_x, snake_y,clocko,yolo = basic_defaults()
    running = True
    while (running):

        #for moving of snake in the game [code starts]
        screen.fill((0, 0, 0))
        '''for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_UP:
                    yolo="snake_y - 14"
                    return yolo
                elif event.key == pygame.K_DOWN:
                    yolo="snake_y + 14"
                    return yolo
                elif event.key == pygame.K_LEFT:
                    yolo="snake_x - 14"
                    return yolo
                elif event.key == pygame.K_RIGHT:
                    yolo="snake_x + 14"
                    return yolo
                else:
                    pass
                '''
        yolo=snake_movements()
        if yolo == None:
            pass
        else:
            #print(type(yolo))
            print(yolo)
            eval(yolo)
            #exec(yolo)
            print(snake_x, snake_y)
        pygame.draw.rect(screen, (102, 255, 51), (snake_x, snake_y, 10, 10), 4)
        pygame.display.update()
        #for moving of snake in the game [code ends]
        time.sleep(1)
        clocko.tick(30)

game_loop()

please help fast as I need to work on the code and need to complete this task thanks in advance

S. Dhar
  • 5
  • 2
  • 2
    Why even use `eval` here? I don't see why that part would possibly need to be a string that's eval'd. – Carcigenicate May 04 '20 at 17:46
  • Does this answer your question? [How do I execute a string containing Python code in Python?](https://stackoverflow.com/questions/701802/how-do-i-execute-a-string-containing-python-code-in-python) – Tomerikoo May 04 '20 at 18:09
  • Please read about how to create a [mre]. The only code you need to show in this question should only be one or two lines. All the rest of your code is completely irrelevant and blurs the focus of your actual question – Tomerikoo May 04 '20 at 18:11

1 Answers1

0

For one, I don't really see why evaluating a string is necessary, instead of just directly executing that code. It would be better to not use eval() and instead just use the code instead.

However, what it looks like what you need is the exec() function. It's similar to eval(), but it allows for assignments/definitions. Read more about exec() vs eval() here.

So eval(yolo) should be changed to exec(yolo).

That being said, it would be cleaner and more efficient to skip using either eval() or exec() altogether.

a.deshpande012
  • 715
  • 7
  • 18