如何仅一次而不是每次迭代地要求用户提供符号值?

时间:2019-03-29 23:54:13

标签: python python-3.x python-3.7

此代码在当前状态下有效。它旨在成为音乐家的换位工具。我想不通如何只向用户询问一次'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()

当前,它要求用户输入他们想要输入的和弦数量,然后使用列表逐一移调它们。当要求符号值时,我不知道如何只要求该值一次。

1 个答案:

答案 0 :(得分:0)

现在,分配duplicate = [ { id:10, name: 'someName1' }, { id:10, name: 'someName2' } ] 的输入语句在while循环中。如果只希望用户提示一次,则需要将输入语句放在两个while循环之前或之间。