【问题标题】:Turning a list number, into another value that is returned [python]将列表编号转换为返回的另一个值 [python]
【发布时间】:2018-11-27 11:40:49
【问题描述】:

我目前正在处理此代码:

def N_C(question,choice):
    N_C_choice_count_max = len(choice)
    N_C_choice_count_min = N_C_choice_count_max - N_C_choice_count_max
    N_C_restart_input = True
    N_C_choices = []
    print(question)
    for i in range (N_C_choice_count_min,N_C_choice_count_max):
        print(N_C_choice_count_min+i+1,":",choice[i])
        str(N_C_choices.append(N_C_choice_count_min+i+1))
    while N_C_restart_input == True:
        N_C_input = input(">")
        if str(N_C_input) in str(N_C_choices):
            break
        else:
            continue
    return(N_C_input)

N_C("question",["choice1","choice2","asmanychoicesasyouwant"])

它按原样工作得很好,但是要选择一个选项,你输入选项的编号,这是有道理的,但我希望它返回他们选择的数字,即与该数字相关的实际选择.

我在想也许有字典的东西可以工作,甚至只是使用提供的选择列表,但我无法弄清楚,不胜感激。

【问题讨论】:

    标签: python function dictionary


    【解决方案1】:

    choice 是一个包含所有答案的列表,N_C_input 是一个字符串形式的数字,与 choice 相关联。

    return choice[int(N_C_input)-1]
    

    返回实际的选择。

    【讨论】:

    • 谢谢,其实挺简单的,就是想把这个程序做完,想不到。
    猜你喜欢
    • 2014-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-19
    • 2022-01-13
    • 2016-09-14
    • 2016-01-02
    • 1970-01-01
    相关资源
    最近更新 更多