Advertisement
timber101

PickrandomCard2 ways

Jan 14th, 2021
1,019
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.41 KB | None
  1. #  pick a random card from cards database
  2. """
  3. from random import randint
  4. import sqlite3
  5.  
  6. connex = sqlite3.connect("computer_cards.db")
  7.  
  8. def readall_cards():  # this is going to get all the records
  9.    allcards = connex.execute("SELECT * FROM computer")
  10.    return allcards.fetchall()
  11.    
  12.  
  13. def pick_random_card():
  14.    the_pack = readall_cards()
  15.    randindx = randint(0, len(the_pack)-1)
  16.    random_card = the_pack[randindx]
  17.    return random_card
  18.    
  19.    
  20. card = pick_random_card()
  21. print(card)
  22.  
  23. connex.close()
  24. """
  25.  
  26. #two dips
  27. from random import randint
  28. import sqlite3
  29.  
  30. connex = sqlite3.connect("computer_cards.db")
  31.  
  32. def pick_rdm_index():  # this is going to count rows in our table
  33.     rowcntsql = connex.execute("SELECT count (*) FROM computer")
  34.     rowcntval = (rowcntsql.fetchall())  #comes back as a list with tuple inside
  35.     #print(rowcntval) # testing
  36.     tgt = (rowcntval[0][0]) # visit the tuple on index 0 and bring back the list item at index [0]
  37.     #print(type(tgt)) # checking its an intger
  38.     #print(tgt)
  39.     return (randint(1,tgt)) # needs to start at row 1 as it is a basic list
  40.    
  41.  
  42. def pick_random_card():
  43.     rndrow = pick_rdm_index()
  44.     #print(rndrow) for testing
  45.     sel_card_sql = "SELECT * FROM computer WHERE rowid = {} ".format(rndrow)
  46.     cardtgt = connex.execute(sel_card_sql)
  47.     return cardtgt.fetchone()
  48.  
  49.    
  50.    
  51. card = pick_random_card()
  52. print(card)
  53.  
Advertisement
RAW Paste Data Copied
Advertisement