So i have been trying to do this with an sql link so that every time you run it you would be asked to log in and your balance would be saved in the mysql database but i cant figure out how to link it so that the balance will be saved in the sql database and i have been searching for hours trying to find a salution.
Here is all my code that i have right now. I have been playing around with it so its a little weird right now but i just cant figure it out.(I have removed sql info for the post)
import random
import pickle
import os
import time
import mysql.connector
os.system("cls")
mydb = mysql.connector.connect(
host="",
user="",
passwd="",
database="",
auth_plugin=''
)
print(mydb)
mycursor = mydb.cursor()
username = input("What is your username?")
password = input("What is your password?")
print("Hello {}.".format(username))
print("Welcome to this site")
Balance = mycursor.execute("SELECT Balance FROM users WHERE username")
time.sleep(1)
pickle.dump(Balance, open("Balance.dat", "wb"))
Balance = pickle.load(open("Balance.dat", "rb"))
print("Your Balance is currently on")
print (float(Balance))
time.sleep(1)
#Write your game of chance functions here
def coin_flip(guess, bet = 5.0):
#coin flip
print("Flipping the coin, your guess was " + guess)
time.sleep(1)
coin = random.randint(0, 1)
#Result
if coin == 1:
print("Tails!")
if coin == 0:
print("Heads!")
#Give winnings or take loss
if coin == 1 and guess == "Tails" or coin == 0 and guess == "Heads":
txt = "You Won {} dollars"
won = bet
print(txt.format(won))
txt = "Your balance is now {} dollars"
NewBalance= Balance + bet
print(txt.format(NewBalance))
else:
txt = "You lost {} dollars"
lost = bet
print(txt.format(lost))
txt = "Your balance is now {} dollars"
NewBalance= Balance - bet
print(txt.format(NewBalance))
#Call your game of chance functions here
guess = input("Please choose Heads or Tails?")
bet = input("How much do you want to bet?")
coin_flip(guess, bet)