Random Choice in python

Asked

Viewed 2,303 times

0

I’m a beginner and I’m creating a program to make tables, but I didn’t want the program to follow a growing order like "1x1, 1x2" and so on. So I used a "Random.Choice" function to show the tables randomly, I created a list to show the tabs on the screen, but I don’t know how to add and check the answer.

import random

tabuada = int(input('Digite o número da tabuada: \n'))
if tabuada == 1:
   print(random.choice(['1x1 = ','1x2 = ','1x3 = ','1x4 = ','1x5 = ','1x6 = ','1x7 = ','1x8 = ','1x9 = ','1x10 = ']))
  • Doubt is still confused and ambiguous

2 answers

2


To read the table value, we will keep the question code:

numero = int(input('Digite o número da tabuada: \n'))

If we want the table 1 to 10, we draw a factor in this interval:

fator = choice(range(1, 11))

Thus, the user will be asked the result of numero times fator:

resposta = int(input(f'Quanto é {numero}x{fator}? '))

And to display the result of the program:

 print('Acertou' if resposta == numero * fator else 'Errooou!')

Getting the complete code:

from random import choice

numero = int(input('Digite o número da tabuada: '))
fator = choice(range(1, 11))

resposta = int(input(f'Quanto é {numero}x{fator}? '))

print('Acertou' if resposta == numero * fator else 'Errooou!')

See working on Repl.it | Github GIST

If the intention is to request all factors from 1 to 10, just use the function shuffle instead of choice to randomly disorder the factors, prompting the user to respond within a repeat loop.

0

From what I understand, you wish implementar a programme which manages each of the tabuadas randomly.

To develop this algorithm, you will need to carry out the draw with the method choice class random.

Also you have to be careful so that the numbers drawn não se repitam.

In view of these observations I developed the following algorithm...

# Cabeçalho do programa
print(f'=' * 36)
print(f'\033[34m{f"Gerador de tabuadas aleatórias":^36}\033[m')
print('=' * 36)

# Importações de bibliotecas
from random import choice

# Capturando e tratando o número digitado:
while True:
    try:
        n = int(input('Gerando tabuada aleatória de: '))
        if n <= 0:
            print('\033[31mValor INVÁLIDO! Digite apenas inteiros maiores que "0"!\033[m')      
        else:
            break
    except:
        print('\033[31mValor INVÁLIDO! Digite apenas inteiros maiores que "0"!\033[m')

# Lista de valores a ser sorteados:
valores = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

# Sorteio e exibição dos valores:
print(f'\033[32mTabuada aleatória de {n}')
for c in range(1, 11):
    x = choice(valores)
    prod = (n * x)
    print(f'{n} x {x} = {prod}')
    # Exclusão do valor sorteado para não incorrer em sorteios duplicados:
    valores.remove(x)

Note the functioning of the algorithm in repl it..

Note that this program has been developed as follows:

The first bloco Code forms the program header.

The second block of code organizes the libraries used. The next block captures and treats the entered value. The next block stores the list from which to draw the values.

The main block of codes is the last one. It is in this code, where the sorteio, the produto of the values and the exibição of the results. Then occurs the remoção of the drawn list value valores, thus avoiding that the amount drawn anteriormente be drawn again.

From this the program will display the tabuada of a certain number n in a non-sequential way and, yes, in a aleatória.

Browser other questions tagged

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