[Tutor] Random ai win glitch

2019-05-01 Thread Cravan
Hi all, 

    I’m a beginner using python3, and recently learnt oop in 
pygame. What I’m trying to create is a tug of war game, but somehow my console 
prints Ai wins even before the player crosses the middle line.Here is my code:

 

newsettings.py:

# Basic colours

BLACK    = (   0,   0,   0)

WHITE    = ( 255, 255, 255)

GREEN    = (   0, 255,   0)

RED  = ( 255,   0,   0)

BLUE = (   0,   0, 255)

YELLOW   = ( 255, 255, 0)

ORANGE   = ( 255, 165, 0)

PURPLE   = ( 100, 0, 100)

 

#settings

WIDTH = 1024

HEIGHT = 768

FPS = 60

TITLE = "War of Tugs"

BGCOLOR = GREEN

 

spritesdata.py:

import pygame as pg

from os import path

import os

from newsettings import *

size = (WIDTH, HEIGHT)

screen = pg.display.set_mode(size)

pg.display.init()

game_folder = os.path.dirname(__file__)

resources_folder = os.path.join(game_folder, 'resources')

stickmanplayer_img = pg.image.load(os.path.join(resources_folder, 
'stickmanplayer.png')).convert()

stickmanai_img = pg.image.load(os.path.join(resources_folder, 
'stickmanai.png')).convert()

#https://www.shutterstock.com/image-vector/stick-figure-pulling-rope-coiled-1140317804?src=c7Vbr97B4rIRsJ9OYUVcLw-1-0

string = pg.sprite.Group()

all_sprites = pg.sprite.Group()

stickman_ai = pg.sprite.Group()

stickman_player = pg.sprite.Group()

AI_wins = 0

Player_wins = 0

class Player(pg.sprite.Sprite):

    def __init__(self, game, x, y):

    self.groups = game.all_sprites

    pg.sprite.Sprite.__init__(self, self.groups)

    self.game = game

    self.image = stickmanplayer_img

    self.image.set_colorkey(BLACK)

    self.rect = self.image.get_rect()

    self.rect.x = x

    self.rect.y = y

    self.dx = 1

    def move_left(self,lol):

    if self.rect.left > 10 and lol.rect.x > 310:

    self.rect.x -= 30

    lol.rect.x -= 30

    else:

    self.rect.left = 10

 

    def update(self):

    if self.rect.right < 770:

    self.rect.x += self.dx

class AI(pg.sprite.Sprite):

    def __init__(self, game, x, y):

    self.groups = game.all_sprites

    pg.sprite.Sprite.__init__(self, self.groups)

    self.game = game

    self.image = stickmanai_img

    self.image.set_colorkey(BLACK)

    self.rect = self.image.get_rect()

    self.rect.x = x

    self.rect.y = y

    self.dx = 1

    def update(self):

    if self.rect.right < 1000:

    self.rect.x += self.dx

 

class String(pg.sprite.Sprite):

    def __init__(self, game, x, y, height = 10, width = 320):

    self.game = game

    self.groups = game.all_sprites

    pg.sprite.Sprite.__init__(self, self.groups)

    self.height = height

    self.width = width

    self.surface = pg.Surface((2 * self.width, 2 * self.height))

    self.surface.fill(YELLOW)

    self.surface.set_colorkey(YELLOW)

    pg.draw.line(self.surface, BLACK, [0,0], [self.width,0], self.height)

    self.image = self.surface

    self.rect = self.image.get_rect()

    self.rect.x = x

    self.rect.y = y

    self.dx = 1

    def move_to_player(self):

    if self.rect.left > 100:

    self.rect.x -= 30

    else:

    self.rect.left = 100

    def update(self):

    if self.rect.right < 1300:

    self.rect.x += self.dx

    def check_win(self,lol):

    global Player_wins

    global AI_wins

    if self.rect.right < lol.rect.x:

    Player_wins += 1

    print("player wins")

    if self.rect.left > lol.rect.x:

    AI_wins += 1

    print("ai wins")

class Middle_Line(pg.sprite.Sprite):

    def __init__(self, game, x):

    self.game = game

    self.groups = game.all_sprites

    pg.sprite.Sprite.__init__(self, self.groups)

    self.width = WIDTH + 100

    self.height = HEIGHT

    self.surface = pg.Surface((2 * self.width, 2 * self.height))

    self.surface.fill(WHITE)

    self.surface.set_colorkey(WHITE)

    pg.draw.line(self.surface, RED, [200, 0], [200, self.height], 5)

    self.image = self.surface

    self.rect = self.image.get_rect()

    self.rect.x = x

main.py:

import pygame as pg

import sys

import random

import math

import time

from os import path

from newsettings import *

from spritesdata import *

clock = pg.time.Clock()

Player_wins = 0

AI_wins = 0

class Game:

    def __init__(self):

    pg.init()

    Player_wins = 0

    AI_wins = 0

    self.screen = pg.display.set_mode((WIDTH, HEIGHT))

    pg.display.set_caption(TITLE)

    self.clock = pg.time.Clock()

    self.time = pg.time.get_ticks()

    pg.key.set_repeat(500, 100)

    self.all_sprites = pg.sprite.Group()

    self.player = Player(self, 249, 384)

    self.ai = AI(self, 550, 430)

    self.middle_line = Middle_Line(self, 300)

    self.string = String(s

[Tutor] Error when trying to insert csv values into a sql table

2019-06-11 Thread Cravan
Here is the stack overflow link: 
https://stackoverflow.com/questions/56540292/error-when-trying-to-insert-csv-values-into-a-sql-table

 

I'm getting a weird error code when I try to store values from a csv into an 
sql table in a movie review assignment.

I have already edited my apostrophes and spacing and looked up examples from 
google to try and resolve my error to no avail. I also ensured that i defined 
DATABASE_URL properly. Sorry for the long traceback error at the end :P Please 
note that my csv values are stored in lists in each cell. They are arranged in 
a single column such as

The Lego Movie;2014;100;tt1490017;7.8

This is my main code

import os

from sqlalchemy import create_engine

from sqlalchemy.orm import scoped_session, sessionmaker

 

engine = create_engine(os.getenv("DATABASE_URL")) # database engine object from 
SQLAlchemy that manages connections to the database

  # DATABASE_URL is an 
environment variable that indicates where the database lives

 

db = scoped_session(sessionmaker(bind=engine)) 

def main():

    f = open("movies.csv","r")

    reader = csv.reader(f)

    for row in f: # loop gives each column a name

    vals = row.split(';')

    title = vals[0]

    year = vals[1]

    runtime = vals[2]

    imdbID = vals[3]

    imdbRating = vals[4]

    db.execute('INSERT INTO movies (Title, Year, Runtime, imdbID, 
imdbRating) VALUES (:title, :year, :runtime, :imdbID, :imdbRating)',

  {'title': title, 'year': year, 'runtime': runtime, 
'imdbID': imdbID, 'imdbRating': imdbRating}) # substitute values from CSV line 
into SQL command, as per this dict

    print(f"Added movie named {title} of {year} lasting {runtime} minutes. 
Its imdbID is {imdbID} and its imdbRating is {imdbRating}.")

This is the sql

CREATE TABLE movies (

  Title SERIAL PRIMARY KEY,

  Year INTEGER NOT NULL,

  Runtime INTEGER NOT NULL

  imdbID VARCHAR NOT NULL,

  imdbRating INTEGER NOT NULL

  );

This is the error i got:

Traceback (most recent call last):

  File 
"/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/sqla

lchemy/engine/base.py", line 1244, in _execute_context

    cursor, statement, parameters, context

  File 
"/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/sqla

lchemy/engine/default.py", line 550, in do_execute

    cursor.execute(statement, parameters)

psycopg2.errors.UndefinedTable: relation "movies" does not exist

LINE 1: INSERT INTO movies (Title, Year, Runtime, imdbID, imdbRating...

The above exception was the direct cause of the following exception:

 

Traceback (most recent call last):

  File "import.py", line 25, in 

    main()

  File "import.py", line 21, in main

    {'title': title, 'year': year, 'runtime': runtime, 'imdbID': imdbID, 
'imdbRating': imd

bRating}) # substitute values from CSV line into SQL command, as per this dict

  File 
"/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/sqla

lchemy/orm/scoping.py", line 162, in do

    return getattr(self.registry(), name)(*args, **kwargs)

  File 
"/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/sqla

lchemy/orm/session.py", line 1268, in execute

    clause, params or {}

  File 
"/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/sqla

lchemy/orm/session.py", line 1268, in execute

    clause, params or {}

  File 
"/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/sqla

lchemy/engine/base.py", line 988, in execute

    return meth(self, multiparams, params)

  File 
"/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/sqla

lchemy/sql/elements.py", line 287, in _execute_on_connection

    return connection._execute_clauseelement(self, multiparams, params)

  File 
"/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/sqla

lchemy/engine/base.py", line 1107, in _execute_clauseelement

    distilled_params,

  File 
"/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/sqla

lchemy/engine/base.py", line 1248, in _execute_context

e, statement, parameters, cursor, context

  File 
"/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/sqla

lchemy/engine/base.py", line 1466, in _handle_dbapi_exception

    util.raise_from_cause(sqlalchemy_exception, exc_info)

  File 
"/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/sqla

lchemy/util/compat.py", line 383, in raise_from_cause

    reraise(type(exception), exception, tb=exc_tb, cause=cause)

  File 
"/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/sqla

lchemy/util/compat.py", line 128, in reraise

    raise value.with_traceback(tb)

  File 
"/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packa

[Tutor] Unexpected result when running flask application.

2019-06-19 Thread Cravan
Hi all,

I am experiencing an unexpected result when I try to run my 
flask application. The movie.html page prints out nothing except those in the 
. This appears on my webpage:

I want it to print out all the stuff related to the movie in my sql table, e.g. 
year, runtime etc. How should I modify my code then? Also, when trying out 
search.html, it prints method not allowed for the requested URL. How should I 
rectify this?

Cravan

 

   {% extends "layout.html" %} {% block title %}

  ERROR 404 NOT FOUND

   [1]Retry!

   {% endblock %} {% block body %}

  ERROR 404 NOT FOUND

   {{message}} {% endblock %}

References

   Visible links
   1. file:///tmp/{{ url_for('login') }}
   {% extends "layout.html" %} {% block title %}

  ERROR 404 NOT FOUND

   [1]Retry!

   {% endblock %} {% block body %}

  ERROR 404 NOT FOUND

   [2]Logout!

   {{message}} {% endblock %}

References

   Visible links
   1. file:///tmp/{{ url_for('movies') }}
   2. file:///tmp/{{ url_for('logout') }}
   {% extends "layout.html" %} {% block title %} Login/Registration {%
   endblock %} {% block body %}

  Hi, welcome to Movie Reviewer. This website is created by yourfavouriteguy.
Kindly Login or Register first!

   Username:
   [1]_
   Password:
   [2]_
   [3]Confirm Registration!

   If you have registered, please login!
   Name:
   [4]_
   Password:
   [5]_
   [6]Login!
  {% block body %} {% endblock %}
   {% extends "layout.html" %} {% block title %} {% endblock %} {% block body
   %}

 Movie Details

   {% for lol in movie_title %}

 Title: {{lol.title}}
 Year: {{lol.year}}
 Runtime: {{lol.runtime}}
 ImdbID: {{lol.imdbID}}
 ImdbRating: {{lol.imdbRating}}

   {% endfor %}

   [1]Back!

   {% endblock %}

   [2]Logout!

References

   Visible links
   1. file:///tmp/{{ url_for('movies') }}
   2. file:///tmp/{{ url_for('logout') }}
   {% block body %}

 All Movies Related to Search:

 {% for movie in movie_specific %}
 * [1]Here's a movie called {{movie.title}} and lasts for
   {{movie.runtime}} minutes. It was shot in the year {{movie.year}} and
   has the imdbID of {{movie.imdbID}}. Its rating is
   {{movie.imdbRating}}. {% endfor %}

   [2]Back!

   [3]Logout!

   {% endblock %}

References

   Visible links
   1. file:///tmp/{{ url_for('movie', movie_title = movie.title) }}
   2. file:///tmp/{{ url_for('movies') }}
   3. file:///tmp/{{ url_for('logout') }}
   {% extends "layout.html" %} {% block title %} Movies {% endblock %} {%
   block body %}

   All Movies

   [1]Search for a movie

 {% for movie in movies %}
 * [2]Here's a movie called {{movie.title}} and lasts for
   {{movie.runtime}} minutes. It was shot in the year {{movie.year}} and
   has the imdbID of {{movie.imdbID}}. Its rating is
   {{movie.imdbRating}}. {% endfor %}

   [3]Logout!

   {% endblock %}

References

   Visible links
   1. file:///tmp/search.html
   2. file:///tmp/{{ url_for('movie', movie_title=movie) }}
   3. file:///tmp/{{ url_for('logout') }}
   {% extends "layout.html" %} {% block title %} {% endblock %} {% block body
   %}

Search:

   [1]_
   {% endblock %}

References

   Visible links
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor