1

I'm relatively new when it comes to python, and do know how to convert strings to integers. However, for some reason I'm unable to when drawing lines from pygame. Before converting the code to integer, it said I had a deprecation warning and required integers instead of the floats I had. I did what it told me, but now I get an error saying int() can't convert non-string with explicit base.

Before conversion:

def draw_board(board):
      pygame.draw.line(screen,WHITE, (WIDTH/3,0),(WIDTH/3,HEIGHT),5)
      pygame.draw.line(screen,WHITE,(WIDTH/1.5,0),(WIDTH/1.5,HEIGHT),5)
      pygame.draw.line(screen,WHITE,(0,HEIGHT/1.5),(WIDTH,HEIGHT/1.5),5)
      pygame.draw.line(screen,WHITE,(0,HEIGHT/3),(WIDTH,HEIGHT/3),5)

After conversion:

def draw_board(board):
      pygame.draw.line(screen,WHITE, (int (WIDTH/3,0), int (WIDTH/3,HEIGHT)),5)
      pygame.draw.line(screen,WHITE,(int (WIDTH/1.5,0), int (WIDTH/1.5,HEIGHT)),5)
      pygame.draw.line(screen,WHITE,(int (0,HEIGHT/1.5), int (WIDTH,HEIGHT/1.5)),5)
      pygame.draw.line(screen,WHITE,(int (0,HEIGHT/3),int (WIDTH,HEIGHT/3)),5)
5
  • 2
    Instead of '/', try '//'. The latter is integer division which should get you what you want Commented Jun 17, 2020 at 22:26
  • You included two values in one "int" call. Second value is interpreted as base. Commented Jun 17, 2020 at 22:31
  • 1
    It should be int(WIDTH/1.5), 0) Commented Jun 17, 2020 at 22:34
  • maybe first convert it - x1 = int(WIDTH/3) y1 = int(0) and later use it line( ..., x1, y1, ...). This way code will be more readable and you will see where you made mistake in original code. You do int (WIDTH/3,0) but you need ( int(WIDTH/3), int(0) ) or ( int(WIDTH/3), 0 ) Commented Jun 17, 2020 at 23:12
  • You're passing too many arguments to int(). Try converting each one separately like: pygame.draw.line(screen, WHITE, (int(WIDTH/3), 0), (int(WIDTH/3), int(HEIGHT)), 5). Commented Jun 17, 2020 at 23:57

1 Answer 1

4
pygame.draw.line(screen,WHITE, (int (WIDTH/3,0), int (WIDTH/3,HEIGHT)),5)

There are two problems here.

(int (WIDTH/3,0), int (WIDTH/3,HEIGHT))

This combines two of your arguments into a tuple - which I'm guessing isn't what you want.

int (WIDTH/3,0)

This passes two arguments to the int function - which isn't what you want either.

For division between two integers, you may/should have the option of using the "floor division" operator e.g.

WIDTH // 3

Otherwise, you'll want something more like:

pygame.draw.line(screen,WHITE,(0, int(HEIGHT/1.5)), (WIDTH, int(HEIGHT/1.5)),5)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.