首页 > 解决方案 > 我正在尝试制作一个递归函数,不断要求用户决定他们想要如何处理 wav 文件

问题描述

请帮我修复这个递归函数,以便我可以在每个阶段成功提示用户。我正在尝试制作一个允许用户混合、更改播放速度和过滤 wav 文件信号的程序。为此,我需要创建一个递归问题,不断提示用户在该程序中使用不同的工具。

def c_function():
    print("you have successfully called c_function")# These print statements are just place holders for the actual functions
    return
def m_function():
    print("you have successfully called m_function")
    return
def f_function():
    print("you have successfully called f_function")
    return
def s_function():
    print("you have successfully called s_function")
    return
"use these functions as selection tools in the recursive function bellow"

user_question1 = input("Select one of the following four options:\n s see the sample rate of the file\n c change the playback speed\n m mix two signals together\n f filter the signal or\n q quit \n : ")

def recursive_main_function():
    if user_question1 == ('c'):
        c_function()
     recursive_main_function()

    if user_question1 == ('m'):
        m_function()
    recursive_main_function()

    if user_question1 == ('f'):
        f_function()
    recursive_main_function()

    else:
        print("Invalid response. Please try again" + user_question1)

        return

标签: pythonrecursionuser-input

解决方案


这是我对相关问题的回答的简短版本。

if您可以使用字典将用户输入与函数匹配,而不是使用 s 的森林。

def c_function(argv):
    print("you have successfully called c_function")# These print statements are just place holders for the actual functions
    return 0
def m_function(argv):
    print("you have successfully called m_function")
    return 0
def f_function(argv):
    print("you have successfully called f_function")
    return 0
def s_function(argv):
    print("you have successfully called s_function")
    return 0

def help_function(argv):
    print('available commands:')
    print('  s      see the sample rate of the file')
    print('  c      change the playback speed')
    print('  m      mix two signals together')
    print('  f      filter the signal')
    print('  help   print the list of commands')
    print('  quit   quit')
    return 0
    
def quit_function(argv):
    env['didntQuit'] = False
    return 0

def notfound_function(argv):
    print('{}: {}: command not found'.format(env['replname'], argv[0]))
    return -1

def print_return_value(ret, argv):
    if ret != 0:
        print('{}: command {} failed with return value {}'.format(env['replname'], argv[0], ret))
env = { 'replname': 'wav-processor-repl', 'prompt': '$ ',  'didntQuit': True }

command_dict = {'c': c_function, 'm': m_function, 'f': f_function, 'h': help_function, 'help': help_function, 'q': quit_function, 'quit': quit_function}

while env['didntQuit']:
    argv = input(env['prompt']).strip().split()            # read
    command = command_dict.get(argv[0], notfound_function) # identify command
    ret = command(argv)                                    # execute command
    print_return_value(ret, argv)                          # error message if command failed

输出:

$ c
you have successfully called c_function
$ f
you have successfully called f_function
$ g
waf-processor-repl: g: command not found
waf-processor-repl: command g failed with return value -1
$ f
you have successfully called f_function
$ h
available commands:
  s      see the sample rate of the file
  c      change the playback speed
  m      mix two signals together
  f      filter the signal
  help   print the list of commands
  quit   quit
$ m
you have successfully called m_function
$ q

推荐阅读