Advertisement
xavicano

Untitled

May 17th, 2020
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.31 KB | None | 0 0
  1. import sqlite3
  2. from time import time
  3. from random import randint
  4.  
  5. conn = sqlite3.connect("computer_cards.db")
  6.  
  7. def read_all_cards():
  8.     result = conn.execute("SELECT * FROM computer")
  9.     return result.fetchall()
  10.    
  11. def pick_card():
  12.     cards = read_all_cards()
  13.     last_picked_card = read_last_picked()
  14.     random_card = cards[randint(0, len(cards) - 1)]
  15.     while random_card[0] == last_picked_card[0]:
  16.         random_card = cards[randint(0, len(cards) - 1)]
  17.     insert_picked(random_card[0])
  18.     return random_card
  19.    
  20. def insert_picked(name):
  21.     insert_sql = "INSERT INTO picked(name, time) VALUES ('{}', {})".format(name, time())
  22.     conn.execute(insert_sql)
  23.     conn.commit()
  24.    
  25. def read_last_picked():
  26.     result = conn.execute("SELECT * FROM picked ORDER BY time DESC")
  27.     return result.fetchone()
  28.    
  29. choosing_player = "1"
  30.  
  31. player = input("Are you player (1) or (2) >")
  32.  
  33. for round in range(5):
  34.     input("Press enter to pick a card when both players are ready >")
  35.     lastCard=last_picked_card()
  36.     card = pick_card()
  37.     card_text = "{}, cores={}, speed={}GHz, RAM={}MB, cost={}$".format(card[0], card[1], card[2], card[3], card[4])
  38.     print(card_text)
  39.     print("Player " + choosing_player + " picks.")
  40.     winner = input("Did you win? (Y)es, (N)o, (D)raw >").lower()
  41.     if winner == "y":
  42.         choosing_player = player
  43.     elif winner == "n":
  44.         choosing_player = "2" if player == "1" else "1"
  45.  
  46. conn.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement