在当前状态下,此代码有效。它旨在成为音乐家的换位工具。我无法弄清楚如何只要求用户输入"sign"值一次,而不是在每次成功迭代后。
import time
import sys
usr_list = ['A','BB','B','C','C#','D','EB','E','F','F#','G','AB']
u_list = []
t_list = []
def f():
usr = int(input('Enter how many chords need to be transposed: '))
while usr!=0:
chord = input('nEnter the chord that needs to be transposed: ').upper()
if chord not in usr_list:
print('Invalid Input')
elif u_list.count(chord) >=1:
print('Already typed')
else:
usr =usr-1
u_list.append(chord)
print('Chords that need to be transposed are:',u_list)
while len(u_list)>0:
chord = (u_list[0])
sign = input('''1.Positive Transposition(+)
2.Negative Transpostion(-)
Your choice: ''')
if sign == '1' or '+':
sign = '+'
time.sleep(0.2)
print('nPositive shift initiating')
time.sleep(0.5)
t_key = int(input('Enter value of transposition: '))
chord_value = int(usr_list.index(chord))
chord_transpose = chord_value + t_key
if chord_transpose >= 12:
chord_transpose = chord_transpose - 12
print(usr_list[chord_transpose])
elif chord_transpose < 12:
del u_list[0]
t_list.append(usr_list[chord_transpose])
print(u_list)
else:
print('Invalid Input, Try Again!')
elif sign == '2' or '-':
sign = '-'
time.sleep(0.2)
print('nNegative initiating')
time.sleep(0.5)
t_key = int(input('Enter value of transposition: '))
chord_value = int(usr_list.index(chord))
chord_transpose = chord_value - t_key
if chord_transpose >= 12:
chord_transpose = chord_transpose + 12
print(usr_list[chord_transpose])
elif chord_transpose < 12:
del u_list[0]
t_list.append(usr_list[chord_transpose])
print(u_list)
else:
print('Invalid')
else:
print('Invalid')
print(t_list)
f()
目前,它要求用户输入他们想要输入的多个和弦,然后使用列表逐个转置它们。 当要求符号值时,我不知道如何只要求一次该值。
现在,分配sign
的输入语句位于您的 while 循环中。如果希望用户只收到一次提示,则需要将输入语句放在 while 循环之前或它们之间。