在当前状态下,此代码有效。它旨在成为音乐家的换位工具。我无法弄清楚如何只询问用户一次'符号'值,而不是每次成功迭代后。
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循环之前或之间。