如何只向用户询问一次符号值而不是在每次迭代中?

How to only ask the user for the sign value once and not in every iteration?

在当前状态下,此代码有效。它旨在成为音乐家的移调工具。我无法弄清楚如何只向用户询问一次 '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 循环之前或它们之间。