SoFunction
Updated on 2025-04-05

Sample code for implementing Spring Festival Gala magic in python

Hello everyone, I watched the magic trick of holding cups in the Spring Festival Gala. As I thought, there must be some math problems, so I tried to implement them in python.

Disrupt the order of spoons, chopsticks, and cups

We can implement it with the following code:

import random
kuaizi = 'Chopsticks'
shaozi = 'spoon'
beizi = 'cup'
l = [kuaizi, shaozi, beizi]
(l)

Chopsticks and left exchange

i = (kuaizi)
if i != 0:
    l[i-1], l[i] = l[i], l[i-1]

The cup and the right side interchange

i = (beizi)
if i != 2:
    l[i+1], l[i] = l[i], l[i+1]

Spoon and left interchange

i = (shaozi)
if i != 0:
    l[i-1], l[i] = l[i], l[i-1]

Pick up something

print("Left hand:",l[0],"Right hand:",l[2])

From the output, we can see that no matter how it runs, the output is holding the cup in the right hand. Let's verify it:

import random
kuaizi = 'Chopsticks'
shaozi = 'spoon'
beizi = 'cup'

j = 0
for _ in range(1000000):
    l = [kuaizi, shaozi, beizi]
    (l)
    # Interchange of chopsticks and left    i = (kuaizi)
    if i != 0:
        l[i-1], l[i] = l[i], l[i-1]
    #The cup and the right side interchange    i = (beizi)
    if i != 2:
        l[i+1], l[i] = l[i], l[i+1]
    #Spoon and left exchange    i = (shaozi)
    if i != 0:
        l[i-1], l[i] = l[i], l[i-1]
    if l[2] == beizi:
        j += 1
print(j/1000000)

The final output is 1.0, which means that the right hand will definitely pick up the cup. Can anyone explain it?

This is the article about the example code for python implementing Spring Festival Gala magic. For more related python Spring Festival Gala magic content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!