Rock Paper Scissors game gone wrong. Random function Error

Question:

I am writing code for a rock paper scissors game, and when i start writing computer choice with the random function, I get this:

TypeError: randint() missing 1 required positional argument: 'b'

Here’s the code:

import random

ai_choice = random.randint([0, 2])

if int(ai_choice) == 0:
  print(rock)
  
if int(ai_choice) == 1:
  print(paper)
  
if int(ai_choice) == 2:
  print(scissors)

I tried looking at the error online but to no avail. I was expecting a normal Rock, Paper, Scissors game.

Asked By: A.H Moussa

||

Answers:

"Missing positional arguments" means that the function takes more parameters than you provided. When this happens, it can be helpful to look up the documentation to see which parameters you need to pass.

randint()‘s documentation states that it needs two parameters, the start and end point. Instead, you’ve passed both in a list as a single parameter. To fix this, just move them out of the list like this:

ai_choice = random.randint(0, 2)
Answered By: Michael M.

The issue occurs because your are calling the randint function with a list. Here is how you would call it to get the desired result:

import random

ai_choice = random.randint(0, 2)

if int(ai_choice) == 0:
  print("rock")
  
if int(ai_choice) == 1:
  print("paper")
  
if int(ai_choice) == 2:
  print("scissors")

Additionally, I added double quotes to your rock, paper and scissors because if the are not formatted as strings, python will look for those variables and realize that they are not defined and give you an error.

Answered By: Marcelo Paco

Fix your random.randint syntax and use a list of choices:

import random

choices = ['Rock', 'Paper', 'Scissors']
ai_choice = random.randint(0, 2)

print(choices[ai_choice])

Output:

Scissors
Answered By: Corralien
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.