Subtracting 12 when the sum of the two variables is greater than or equal to 12

I come from the background music, so I was interested in introducing the theory of music in a Python script.

Basically, musical note variables are assigned to numbers (C = 0, C sharp = 1, etc.). However, the theory of musical sets only works up to the number 11, since B = 11, and the next C will again be = 0.

I have already assigned some variables, they look like this.

# pitch classes
Bs = C = 0
Cs = Db = 1
D = 2
Ds = Eb = 3
E = Fb = 4
F = Es = 5
Fs = Gb = 6
G = 7
Gs = Ab = 8
A = 9
As = Bb = 10
B = Cb = 11

# intervals
m2 = 1 
mj2 = 2 
m3 = 3 
mj3 = 4
P4 = 5 
T = 6
P5 = 7
m6 = 8
mj6 = 9
m7 = 10
mj7 = 11

I want to be able to add notes and intervals together, for example, B plus perfect 5. Usually it will be 11 + 7 = 18, but I want it to be 6 (since 6 = F sharp, and B and F sharp are ideal 5 place).

I think I need something like that, but I have no idea how to implement it.

if answer >= 12:
    answer - 12

Does anyone have any ideas? Is there a better way to do this?

+4
3

modulo, %, (. ):

print((11 + 7) % 12)

:

def add_wrap_overflow(x,y):
    return (x+y) % 12
+4

; answer - 12 , answer - 12 - .

, ( ):

answer = answer - 12.

Python ( , ):

answer -= 12.

modulo :

answer = answer % 12 answer %= 12

, modulo 12 , range(0, 12) ( 0 11).

0

if (answer >= 12):
    answer = answer - 12
0

All Articles