random choice in python

0

I am a beginner and I am creating a program to make tables, but I did not want the program to follow a growing order like "1x1, 1x2" and so on. So I used a "random.choice" function to randomly display the tables, I created a list to show the tables on the screen, but I do not know how to add and verify 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 = ']))
    
asked by anonymous 21.05.2018 / 01:36

1 answer

0

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

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

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

fator = choice(range(1, 11))

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

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

And, to display the program result:

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

Getting the full 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 at Repl.it | GitHub GIST

If the intention is to request all factors from 1 to 10, simply use the shuffle function instead of choice to randomly clutter the factors, prompting the user for the response within a loop repetition

    
21.05.2018 / 03:25