Random.choices() Help

Question:
I am making a lottery esq. game (you don’t win anything to be clear) and when you win I am trying to add to the winMoney variable and I want different chances so I am using the random.choice() function. It throws the error when I actually win of TypeError: unsupported operand type(s) for += ‘int’ and ‘list’ is there any fix. Also if you can could you make it so higher winning have lower chances of winning and vice versa.

Repl link:
https://replit.com/@SalladShooter/Scratchoff-Cycles-Lottery

import random
import extrapy as e
import time
import sys
import os
import threading

from replit import db

import random

BLACK = "\033[0;30m"
RED = "\033[0;31m"
GREEN = "\033[0;32m"
BROWN = "\033[0;33m"
BLUE = "\033[0;34m"
PURPLE = "\033[0;35m"
CYAN = "\033[0;36m"
LIGHT_GRAY = "\033[0;37m"
DARK_GRAY = "\033[1;30m"
LIGHT_RED = "\033[1;31m"
LIGHT_GREEN = "\033[1;32m"
YELLOW = "\033[1;33m"
LIGHT_BLUE = "\033[1;34m"
LIGHT_PURPLE = "\033[1;35m"
LIGHT_CYAN = "\033[1;36m"
LIGHT_WHITE = "\033[1;37m"
BOLD = "\033[1m"
FAINT = "\033[2m"
ITALIC = "\033[3m"
UNDERLINE = "\033[4m"
BLINK = "\033[5m"
NEGATIVE = "\033[7m"
CROSSED = "\033[9m"
END = "\033[0m"

running = True
username = os.environ['REPL_OWNER']
if username in db:
    money = db[username]["money"]
else:
    db[username] = {"money": 100}
    money = db[username]["money"]

def enter():
  input(f"\nPress | {BLUE}Enter{LIGHT_WHITE} | to continue...")

def scratch():
  input(f"\nPress | {BLUE}Enter{LIGHT_WHITE} | to scratchoff...")

def bar():
  print("------------------------")

def write(write):
  for i in write:
    sys.stdout.write(i)
    sys.stdout.flush()
    time.sleep(.05)

def titleBlink():
  for _ in range(5):
    text = f"{BOLD}Scratchoff Cycles Lottery!"
    sys.stdout.write(f'\r{text}')
    sys.stdout.flush()
    time.sleep(0.5)
    b = "Loading"
    sys.stdout.write('\r' + ' ' * len(text))
    sys.stdout.flush()
    time.sleep(0.5)

print(f"{BOLD}Welcome To")

titleBlink()
running = False

print(f"\r{BOLD}Scratchoff Cycles Lottery!{END}")

bar()
enter()

e.clear()

def printTicket(winNum, spot1, spot2, spot3, spot4, spot5, spot6, spot7, spot8):
  print("___________________________________")
  print("|    Scratchoff Cycles Lottery    |")
  print("|-------|-------------------------|")
  print("| Win # |   _____________________ |")
  print("|-------|   |    |    |    |    | |")
  print("|       |   --------------------- |")
  print("|       |   |    |    |    |    | |")
  print("|       |   --------------------- |")
  print("-----------------------------------")
  
  scratch()
  e.clear()
  
  print("___________________________________")
  print("|    Scratchoff Cycles Lottery    |")
  print("|-------|-------------------------|")
  print("| Win # |   _____________________ |")
  print(f"|-------|   | {BOLD}{spot1}{END} | {BOLD}{spot2}{END} | {BOLD}{spot3}{END} | {BOLD}{spot4}{END} | |")
  print("|       |   --------------------- |")
  print(f"|  {BOLD}{winNum}{END}   |   | {BOLD}{spot5}{END} | {BOLD}{spot6}{END} | {BOLD}{spot7}{END} | {BOLD}{spot8}{END} | |")
  print("|       |   --------------------- |")
  print("-----------------------------------")

  winMoney = 0

  amounts = []
  chances = []
  
  for j in range(1000):
    amounts.append(random.randint(10,100000))
    chances.append(random.randint(1, 30))
  
  if spot1 == winNum:
    winMoney += random.choices(amounts, weights=chances, k = 14)

  if spot2 == winNum:
    winMoney += random.choices(amounts, weights=chances, k = 14)

  if spot3 == winNum:
    winMoney += random.choices(amounts, weights=chances, k = 14)
    
  if spot4 == winNum:
    winMoney += random.choices(amounts, weights=chances, k = 14)
    
  if spot5 == winNum:
    winMoney += random.choices(amounts, weights=chances, k = 14)
    
  if spot6 == winNum:
    winMoney += random.choices(amounts, weights=chances, k = 14)
    
  if spot7 == winNum:
    winMoney += random.choices(amounts, weights=chances, k = 14)
    
  if spot8 == winNum:
    winMoney += random.choices(amounts, weights=chances, k = 14)
    
  print(f"You won {BOLD}🌀{winMoney}{END}")
  enter()
  e.clear()
  
  print(f"\r{BOLD}Scratchoff Cycles Lottery!{END}")
  print(f"You have {BOLD}🌀{money}{END}")
  print()
  print("What do you want to do?")
  print(" |1| Buy Ticket")
  print(" |2| Leaderboard")
  choice = input("\n> ")
  if choice == "1":
    buyTicket()
  elif choice == "2":
    leaderboard()

def buyTicket():
  global money, username
  money -= 5
  db[username]["money"] = money
  e.clear()
  winNum = random.randint(1,99)
  spot1 = random.randint(1,99)
  spot2 = random.randint(1,99)
  spot3 = random.randint(1,99)
  spot4 = random.randint(1,99)
  spot5 = random.randint(1,99)
  spot6 = random.randint(1,99)
  spot7 = random.randint(1,99)
  spot8 = random.randint(1,99)
  if winNum < 10:
    winNum = f"0{winNum}"
  if spot1 < 10:
    spot1 = f"0{spot1}"
  if spot2 < 10:
    spot2 = f"0{spot2}"
  if spot3 < 10:
    spot3 = f"0{spot3}"
  if spot4 < 10:
    spot4 = f"0{spot4}"
  if spot5 < 10:
    spot5 = f"0{spot5}"
  if spot6 < 10:
    spot6 = f"0{spot6}"
  if spot7 < 10:
    spot7 = f"0{spot7}"
  if spot8 < 10:
    spot8 = f"0{spot8}"
  printTicket(winNum, spot1, spot2, spot3, spot4, spot5, spot6, spot7, spot8)
  

def leaderboard():
  pass

class Game:
  print(f"\r{BOLD}Scratchoff Cycles Lottery!{END}")
  print(f"You have {BOLD}🌀{money}{END}")
  print()
  print("What do you want to do?")
  print(" |1| Buy Ticket")
  print(" |2| Leaderboard")
  choice = input("\n> ")
  if choice == "1":
    buyTicket()
  elif choice == "2":
    leaderboard()

Game()

So where is the code for winning? Glad you put the full snippet but when you put the link to your Repl it’d just be more helpful to post the code that’s erroring. Speaking of errors please also post the full error with the Traceback if possible.

BTW replit-py has a clear function and it’s the fastest clear function I’ve tested in Python, so

from replit import clear, db

Also, why do you import random twice?

1 Like

@QwertyQwerty88 sorry here is the not working code:

def printTicket(winNum, spot1, spot2, spot3, spot4, spot5, spot6, spot7, spot8):
  print("___________________________________")
  print("|    Scratchoff Cycles Lottery    |")
  print("|-------|-------------------------|")
  print("| Win # |   _____________________ |")
  print("|-------|   |    |    |    |    | |")
  print("|       |   --------------------- |")
  print("|       |   |    |    |    |    | |")
  print("|       |   --------------------- |")
  print("-----------------------------------")
  
  scratch()
  e.clear()
  
  print("___________________________________")
  print("|    Scratchoff Cycles Lottery    |")
  print("|-------|-------------------------|")
  print("| Win # |   _____________________ |")
  print(f"|-------|   | {BOLD}{spot1}{END} | {BOLD}{spot2}{END} | {BOLD}{spot3}{END} | {BOLD}{spot4}{END} | |")
  print("|       |   --------------------- |")
  print(f"|  {BOLD}{winNum}{END}   |   | {BOLD}{spot5}{END} | {BOLD}{spot6}{END} | {BOLD}{spot7}{END} | {BOLD}{spot8}{END} | |")
  print("|       |   --------------------- |")
  print("-----------------------------------")

  winMoney = 0

  amounts = []
  chances = []
  
  for j in range(1000):
    amounts.append(random.randint(10,100000))
    chances.append(random.randint(1, 30))
  
  if spot1 == winNum:
    winMoney += random.choices(amounts, weights=chances, k = 14)

  if spot2 == winNum:
    winMoney += random.choices(amounts, weights=chances, k = 14)

  if spot3 == winNum:
    winMoney += random.choices(amounts, weights=chances, k = 14)
    
  if spot4 == winNum:
    winMoney += random.choices(amounts, weights=chances, k = 14)
    
  if spot5 == winNum:
    winMoney += random.choices(amounts, weights=chances, k = 14)
    
  if spot6 == winNum:
    winMoney += random.choices(amounts, weights=chances, k = 14)
    
  if spot7 == winNum:
    winMoney += random.choices(amounts, weights=chances, k = 14)
    
  if spot8 == winNum:
    winMoney += random.choices(amounts, weights=chances, k = 14)
    
  print(f"You won {BOLD}🌀{winMoney}{END}")
  enter()
  e.clear()

I don’t know :man_shrugging:. I just didn’t see it XD.

I think the problem is that you’re using random.choices and not random.choice

5 Likes

@QwertyQwerty88 no AFAIK its random.choices(), see it here: Python Random choices() Method (either that or w3schools is wrong).

1 Like

Pretty sure there are two functions, choice and choices.

I’ve used random.choice before. I think random.choices returns a list.

Edit: yep

3 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.