I would like to know why python is saying that I have provided 3 arguments in this role?

Asked

Viewed 50 times

0

Code:

print("Jogo do chute...")

import random

numeroaleatorio = random.randrange(100)
chutes = 0
r = True

while r:
    chute = int(input("Digite um número de zero(0) a cem(100): "))
    if chute > numeroaleatorio:
        print("Número muito alto...")
        chutes +=1

    elif chute < numeroaleatorio:
        print("Número muito baixo...")
        chutes +=1

    elif chute == numeroaleatorio:
        print("Você acertou...\nA quantidade de chutes que você deu foram %i"%chutes)
        break

Error message:

Jogo do chute...
Traceback (most recent call last):
  File "y.py", line 41, in <module>
    numeroaleatorio = random.choice(1, 100)
TypeError: choice() takes 2 positional arguments but 3 were given

Seen this because python is saying that I provided 3 arguments being that I only provided two arguments '1' and '100' ?

  • 2

    In error it displays the code: numeroaleatorio = random.choice(1, 100) and in the example is with the code numeroaleatorio = random.randrange(100). Why?

2 answers

3


In python 3 Voce should generate the random value Voce wants in this way:

randrange:

from random import randrange
rnd = randrange(1,100)

Output:

print(rnd)
92

Or else:

Choice:

from random import choice
rnd = choice(range(1,100))

Output:

print (rnd)  
61

Run the code in repl.it

1

The intention is to choose a number from 1 to 100?

You can use

random.randrange(1, 100);

//random.randrange([start], stop[, step])

or

random.choice(range(1, 100, 1))

//random.choice(range(start, stop, step))

Browser other questions tagged

You are not signed in. Login or sign up in order to post.