如何使用python中作为输入变量给出的值调用dict值 大多数使用的语言={ 1.“普通话”, 2:“英语”, 3:“印度斯坦”, 4:“西班牙语”, 5:“阿拉伯文”, 6.“马来语”, 7:“俄语”, 8.“孟加拉语”, 9:“葡萄牙语”, 10:“法语” } 打印(“10种最流行的语言如下:”) 打印(大多数使用的语言) def learn_languageinfo_choice(): num=int(输入(“请输入您想进一步了解的语言的对应键:”) 对于键,大多数语言中使用的值。项() 如果str(num).isdigit和len(str(num))>0且num0: 打印(“您选择了“+值(num)+”) 其他: 打印(“您必须选择与所选语言相关的密钥。”) 学习语言信息选择() 学习语言信息选择()

如何使用python中作为输入变量给出的值调用dict值 大多数使用的语言={ 1.“普通话”, 2:“英语”, 3:“印度斯坦”, 4:“西班牙语”, 5:“阿拉伯文”, 6.“马来语”, 7:“俄语”, 8.“孟加拉语”, 9:“葡萄牙语”, 10:“法语” } 打印(“10种最流行的语言如下:”) 打印(大多数使用的语言) def learn_languageinfo_choice(): num=int(输入(“请输入您想进一步了解的语言的对应键:”) 对于键,大多数语言中使用的值。项() 如果str(num).isdigit和len(str(num))>0且num0: 打印(“您选择了“+值(num)+”) 其他: 打印(“您必须选择与所选语言相关的密钥。”) 学习语言信息选择() 学习语言信息选择(),python,python-3.x,list,dictionary,Python,Python 3.x,List,Dictionary,因此,我面临的主要问题是,在定义字典之后,我需要使用用户输入调用dict中的值。 一旦我将输入收集为num,我就不知道如何在函数中调用它。一旦我发现num是一个数字,介于1和10之间,我就尝试继续使用if语句,说明用户选择的语言,但我遇到了以下错误:TypeError:“str”对象不可调用 我真的不知道如何调用特定的dict值,而不使用dict.items()中的key,value: 但如果有人知道更好的方法,请发表评论 另外,如果用户输入是按字母顺序排列的,那么我会遇到错误ValueErro

因此,我面临的主要问题是,在定义字典之后,我需要使用用户输入调用dict中的值。
一旦我将输入收集为num,我就不知道如何在函数中调用它。一旦我发现num是一个数字,介于1和10之间,我就尝试继续使用if语句,说明用户选择的语言,但我遇到了以下错误:TypeError:“str”对象不可调用

我真的不知道如何调用特定的dict值,而不使用dict.items()中的key,value:
但如果有人知道更好的方法,请发表评论

另外,如果用户输入是按字母顺序排列的,那么我会遇到错误ValueError:invalid literal for int(),以10为基数:“a”


如果我能得到一些解释,那就太好了,谢谢

您有太多不必要(和不正确)的条件检查。直接考虑字典中的值:

most_used_languages = { 
    1: "Mandarin Chinese", 
    2: "English",
    3: "Hindustani",
    4: "Spanish",
    5: "Arabic",
    6: "Malay",
    7: "Russian",
    8: "Bengali",
    9: "Portuguese",
    10: "French"
}
print("The 10 most popular languages are as follows: ")
print(most_used_languages)
def learn_languageinfo_choice():
    num = int(input("Please enter the corresponding key to the language you would like to learn more about: "))
    for key, value in most_used_languages.items():
        if str(num).isdigit and len(str(num)) > 0 and num < 11 and num > 0:
            print("You chose " +  value(num) + ".")
        else:
            print("You must choose a key that relates to the language of your choice.")
            learn_languageinfo_choice()
learn_languageinfo_choice()
<>为了使函数真正有用,还考虑返回收集的值<代码> Num < /C>或一些不正确输入的哨兵(例如,-1)。p> 最后,如果用户输入非整数值,程序仍将崩溃(在
int
)。使用
try/except
避免崩溃。

试试这个

def learn_languageinfo_choice():
    num = int(input("Please enter the corresponding key...: "))
    if num in most_used_languages:
        # Note the use of format string
        print(f"You chose {most_used_languages[num]}.")
    else:
        print("You must choose a key...")
most_used_languages = { 
    1: "Mandarin Chinese", 
    2: "English",
    3: "Hindustani",
    4: "Spanish",
    5: "Arabic",
    6: "Malay",
    7: "Russian",
    8: "Bengali",
    9: "Portuguese",
    10: "French"
}

def learn_languageinfo_choice(most_used_languages):
    num = int(input("Please enter the corresponding key to the language you would like to learn more about: "))
    try:
        print(most_used_languages[num])
    except KeyError:
        print('You must choose a key between 1-10 that relates to the language of your choice.')
        learn_languageinfo_choice(most_used_languages)
大多数使用的语言={
1.“普通话”,
2:“英语”,
3:“印度斯坦”,
4:“西班牙语”,
5:“阿拉伯文”,
6.“马来语”,
7:“俄语”,
8.“孟加拉语”,
9:“葡萄牙语”,
10:“法语”
}
打印(“10种最流行的语言如下:”)
打印(大多数使用的语言)
def learn_languageinfo_choice():
num=int(输入(“请输入您想进一步了解的语言的对应键:”)
如果str(num).isdigit()和len(str(num))>0且num<11且num>0:
打印(“您选择“+大多数使用的语言[num]+”)
其他:
打印(“您必须选择与所选语言相关的密钥。”)
学习语言信息选择()

欢迎来到StackOverflow

您可以发布错误消息,但也应该说明它发生在哪一行。完整回溯如下所示:

most_used_languages = { 
    1: "Mandarin Chinese", 
    2: "English",
    3: "Hindustani",
    4: "Spanish",
    5: "Arabic",
    6: "Malay",
    7: "Russian",
    8: "Bengali",
    9: "Portuguese",
    10: "French"
}
print("The 10 most popular languages are as follows: ")
print(most_used_languages)
def learn_languageinfo_choice():
    num = int(input("Please enter the corresponding key to the language you would like to learn more about: "))
    if str(num).isdigit() and len(str(num)) > 0 and num < 11 and num > 0:
        print("You chose " +  most_used_languages[num] + ".")
    else:
        print("You must choose a key that relates to the language of your choice.")
learn_languageinfo_choice()

为什么您需要迭代大多数使用的语言?尝试
print(大多数使用的语言[num])如果num在其他大多数使用的语言中打印('Nope!')
如果str(num)。isdigit和len(str(num))>0和num<11和num>0:如果str(num)在范围(1,11)内,您可以说
if-str(num)你需要考虑<代码>的部分尝试/除< /代码>,我投票赞成这个答案,因为它确实清楚了所有的OP。
Traceback (most recent call last):
  File "stack overflow.py", line 25, in <module>
    learn_languageinfo_choice()
  File "stack overflow.py", line 21, in learn_languageinfo_choice
    print("You chose " +  value(num) + ".")
TypeError: 'str' object is not callable
languages = ["Mandarin Chinese", "English", "Hindustani",
             "Spanish", "Arabic", "Malay", "Russian",
             "Bengali", "Portuguese", "French"]

print("The 10 most popular languages are as follows: ")
print(languages)

def learn_languageinfo_choice():
    # Note: you can still crash this input statement if you do not
    # enter characters which can be converted to an integer.
    num = int(input("Please enter the corresponding key to the language you would like to learn more about: "))
    if 1 <= num <= len(languages):
        choice = languages[num-1] # lists are indexed starting from zero
        print("You chose " + str(num) + ", " + choice + ".")
    else:
        print("You must choose a key that relates to the language of your choice.")
        learn_languageinfo_choice() # recursion can be hazardous...
    return choice # you defined a function, it should return a valid result

learn_languageinfo_choice()