一位朋友给了我一个想法,写一个程序,询问你英语单词,你必须用德语和反向翻译它们。
因此程序会询问一个单词并检查输入是否与实际翻译匹配。
因此,用户输入他/她想要学习的单元。此输入存储在变量中。现在我有一个单元1的单词列表。然后程序随机选择列表中的单词。然后打印该单词并且用户输入他的答案。然后使用.index我找到所述单词的位置并在第二个列表中搜索相同的位置。
我的问题是单位输入被视为字符串而不是我所制作的列表之一。因此,用户只需要从此输入中获取信件。
我想以某种方式让这个输入等于我拥有和工作的列表的名称。(列表实际上更大,我剪切它们使它更容易阅读)
代码在这里:
import random
U1_E = ["consider (sb) to be …","describe (sb) as ...","proud to + infinitive"
]
U1_D = ["erachten, wähnen","erachten, wähnen","beschreiben"
]
U5_E = ["confortable","crowded","delicious","efficient","fashionable",
]
U5_D = ["bequem","überfüllt","köstlich","effizient","modisch"
]
Num = ["first","second","third","forth","fifth","sixth","7th","8th","9th",
]
Unit_Eng = ["U1_E","U2_E","U3_E","U4_E","U5_E","U6_E","U7_E","U8_E"
]
good_points = 0
bad_points = 0
Name_of_Agent = input("Can you please give me your name?")
Select_Unit = input(Name_of_Agent + ",which Unit would you like to train? [Ux_E/Ux_D]")
Num_of_Words = int(input("How many words would you like?"))
if (Select_Unit in Unit_Eng):
for i in range(0,Num_of_Words):
Word_E = random.choice(Select_Unit)
Select_Unit_oposite = Select_Unit[:3] + "D"
Word_D = Select_Unit_oposite[Select_Unit.index(Word_E)]
if (input("The "+Num[i]+" Word is:"+Word_E+":") == Word_D):
print("Well done, you got it right!")
good_points = good_points + 1
else:
print("The word you gave is wrong, the right answer is:", Word_D)
bad_points = bad_points + 1
print("Your total score is:",good_points- bad_points)
input("Press Enter to close the program")
答案 0 :(得分:1)
问题在于您尝试使用Select_Unit
作为列表而不是您要访问的列表的名称。
有几种不同的方法可以实现这一点,如果你想进一步提升你的Python知识,你应该尝试不同的方法。
一种让人想起的方法是将单位存储在列表的Python字典中而不是一系列列表中,这样您就可以立即访问单位名称后面的值(单位)已经确认它存在于您的单位中。
它可能类似于以下内容
units = {"unit_1": ["word_1", "word_2", ...], "unit_2": ...}
# get input including the unit they want
if unit in units:
chosen_unit = units[unit]
# continue to do whatever you need to extract the information now that you have the chosen unit.