Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 战舰游戏。。数据库和存储玩家数据的几个问题_Python_Database_Sqlite - Fatal编程技术网

Python 战舰游戏。。数据库和存储玩家数据的几个问题

Python 战舰游戏。。数据库和存储玩家数据的几个问题,python,database,sqlite,Python,Database,Sqlite,我正在尝试用python创建一个战舰游戏,该游戏包含以下内容: SQLite数据库 数据库中存储的用户登录名和密码、赢得的游戏和高分 登录时显示赢得的游戏和高分 到目前为止,我在数据库中存储数据时遇到了问题。如果有人对我下一步的方向有任何帮助或指导,我将不胜感激 from random import randint import getpass import sqlite3 #creating the database using SQLite def createTable(): d

我正在尝试用python创建一个战舰游戏,该游戏包含以下内容:

SQLite数据库 数据库中存储的用户登录名和密码、赢得的游戏和高分 登录时显示赢得的游戏和高分 到目前为止,我在数据库中存储数据时遇到了问题。如果有人对我下一步的方向有任何帮助或指导,我将不胜感激

from random import randint
import getpass
import sqlite3

#creating the database using SQLite
def createTable():
    db = sqlite3.connect(':memory:')
    db = sqlite3.connect('MyBSdb') #this folder was created
    cursor = db.cursor()
    cursor.execute('''CREATE TABLE IF NOT EXISTS BSTable(name TEXT, password TEXT, wins INT, highscore INT)''')
    db.commit()
    db.close()

def save(name,password):
    db = sqlite3.connect(':memory:')
    db = sqlite3.connect('MyBSdb')
    cursor = db.cursor()
    cursor.execute('''INSERT INTO BSTable(name,password,wins,highscore) values(?,?)''', (name,password,wins,highscore))
    db.commit()
    db.close()

def get():
    db = sqlite3.connect(':memory:')
    db = sqlite3.connect('MyBSdb')
    cursor = db.cursor()
    cursor.execute('''SELECT name,password,wins,highscore from myTable''')
    results = cursor.fetchall()
    for result in results:
        print (result)


if __name__  == '__main__':
    createTable()
    save('users','passw')
    get()

def writeFile():
    #create file handle, open file for append
    file = open('Battleship.txt', 'a')
    text = 'Bob,hello'
    #write string s to file
    file.write(text + '\n')
    #close file handle after write operation
    file.close()

def readFile():
    #create file handle, open file for append
    file = open('Battleship.txt', 'r')
    text = file.readlines()
    for line in text:
        print(line.split(",")[0])
        print(line.split(",")[1])
    #close file handle after write operation
    file.close()

if __name__  == '__main__':
    writeFile()
    readFile()

#creating a login and password
users = {}
status = ""

def displayMenu():
    status = input("Are you registered user? y/n? ")
    if status == "y":
        oldUser()
    elif status == "n":
        newUser()

def newUser():
    createLogin = input("Create login name: ")

    if createLogin in users:
        print("\nLogin name already exist!\n")
    else:
         createPassw = input("Create password: ")
            users[createLogin] = createPassw
        print("\nUser created\n")

def oldUser():
    login = input("Enter login name: ")
    passw = input("Enter password: ")

    if login in users and users[login] == passw:
        print("\nLogin successful!\n")
    else:
        print("\nUser doesn't exist or wrong password!\n")

while status != "q":
   displayMenu() #cant stop the loop here or figure out why it wont move on

#building the gameboard
board = []

for x in range(5):
    board.append(["-"] * 5)

def print_board(board):
    print(" ", " ".join("12345"))
    for letter, row in zip("ABCDE", board):
        print(letter, " ".join(row))

#beginning the game
print("Let's play Battleship!")
print("Find and sink the ship!")
print_board(board)

score = 0 #using this to keep a score to store in the database
win = True

def wins():
    if win == True:
       win = score + 1 

def random_row(board): 
    return randint(0, len(board) - 1)
def random_col(board):
    return randint(0, len(board[0]) - 1)

ship_row = random_row(board)
ship_col = random_col(board)

#the numbers and letters in this section are not aligning with the grid I created
for turn in range(9):
    print ("Turn"), turn
    guess_row = int(input("Guess Row:"))
    guess_col = int(
        input("Guess Col:"))

    if guess_row == ship_row and guess_col == ship_col:
        win == True
        print("Congratulations! You sunk my battleship!")
        print("Your score is " + score ) #keeping score but how to save in db?
        break
    else:
        if (guess_row < 0 or guess_row > 5) or (guess_col < 0 or guess_col > 5):
            print("Oops, that's not even in the ocean.")
        elif(board[guess_row][guess_col] == "X"):
            print("You guessed that one already.")
        else:
            print("You missed my battleship!")
            board[guess_row][guess_col] = "X"
    if turn == 8:
        print("Game Over")
        print("Your final score is " + score ) 
    turn =+ 1
    print_board(board)
    #how to display games won and high score when user logs in? 

我相信您的问题是,要解析的值的数量是2,而您给出了4个值,期望4个列有4个值

尝试使用:-

cursor.execute('''INSERT INTO BSTable(name,password,wins,highscore) values(?,?,?,?)''', (name,password,wins,highscore))