I teach Python, I want to write a script for divination by Tarot.

There are map values ​​in the dictionary, where the key is the position in the layout for each map.

I understood how to randomly deduce the cards from the list, but I don’t understand what needs to be done so that the key for the first cycle for displays is "1", the second for the key "2" and so on. How to do this with if, else?

import random mag = {1: 'Маг говорит, всё хорошо', 2: 'Маг говорит, всё плохо!'} shut = {1: 'Шут говорит, всё хорошо', 2: 'Шут говорит, всё плохо!'} jritsa = {1: 'Жрица говорит, всё хорошо', 2: 'Жрица говорит, всё плохо!'} emperor = {1: 'Император говорит, всё хорошо', 2: 'Император говорит, всё плохо!'} tarot = [mag, shut, jritsa, emperor] for i in random.sample(tarot, 2): if i: print('Значение карты на 1-ой позиции:', i.get(1)) else: print('Значение карты на 2-ой позиции:', i.get(2)) 

Now the code simply displays the cards by the first key in the dictionary.

  • Give an example of the result you want to get. - Anton Komyshan

1 answer 1

Option: Change dictionaries for tuples:

 mag = ("mag say good", "mag say bad") 

etc.

 sample_length = 2 sample = random.sample(tarot, sample_length) for i in range(sample_length): print("Card value:", sample[i][i%2]) 

In general, you just need to check the parity of the next iteration, so you need a loop with a counter, i%2 is the remainder of dividing by 2, and it does this.

  • And if the values ​​are not two, but five? Then parity check is not suitable. - Alexander Levashov
  • @ Alexander Levashov is perfect. The value index remains at i % 5 . - andy.37
  • Thank you very much! - Alexander Levashov