Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # pick a random card from cards database
- """
- from random import randint
- import sqlite3
- connex = sqlite3.connect("computer_cards.db")
- def readall_cards(): # this is going to get all the records
- allcards = connex.execute("SELECT * FROM computer")
- return allcards.fetchall()
- def pick_random_card():
- the_pack = readall_cards()
- randindx = randint(0, len(the_pack)-1)
- random_card = the_pack[randindx]
- return random_card
- card = pick_random_card()
- print(card)
- connex.close()
- """
- #two dips
- from random import randint
- import sqlite3
- connex = sqlite3.connect("computer_cards.db")
- def pick_rdm_index(): # this is going to count rows in our table
- rowcntsql = connex.execute("SELECT count (*) FROM computer")
- rowcntval = (rowcntsql.fetchall()) #comes back as a list with tuple inside
- #print(rowcntval) # testing
- tgt = (rowcntval[0][0]) # visit the tuple on index 0 and bring back the list item at index [0]
- #print(type(tgt)) # checking its an intger
- #print(tgt)
- return (randint(1,tgt)) # needs to start at row 1 as it is a basic list
- def pick_random_card():
- rndrow = pick_rdm_index()
- #print(rndrow) for testing
- sel_card_sql = "SELECT * FROM computer WHERE rowid = {} ".format(rndrow)
- cardtgt = connex.execute(sel_card_sql)
- return cardtgt.fetchone()
- card = pick_random_card()
- print(card)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement