简体   繁体   English

输入应等于列表,但不等于

[英]Input should be equal to a list, but isn't

A friend gave me the idea to write a program that asks you English words and you have to translate them in German and reverse. 一个朋友给我的主意是编写一个程序来询问您英文单词,然后您必须将它们翻译成德语并反向翻译。

So the program asks a word and checks the input if it matches the actual translation. 因此,程序会询问一个单词并检查输入是否与实际翻译匹配。

So the user gives an input of which Unit he/she wants to study. 因此,用户输入他/她想学习哪个单元的信息。 This input is stored in a variable. 此输入存储在变量中。 Now I have a list of the words of Unit 1. Then the program randomly chooses a word in the List. 现在,我有了单元1的单词列表。然后程序在列表中随机选择一个单词。 This word is then printed and the user enters his answer. 然后打印该单词,然后用户输入他的答案。 Then using .index I find the position of the word asked and search for the same position in a second list. 然后使用.index查找所要单词的位置,并在第二个列表中搜索相同的位置。

My problem is that the Unit input is seen as a string instead of being one of the lists I made. 我的问题是,单位输入被视为字符串,而不是我创建的列表之一。 So then the user just gets asked for letter from this input. 因此,仅从该输入中要求用户输入字母。

I want to somehow have this input equal the name of a list I have and work.(The Lists are actually larger, I cut them so it is a little easier to read) 我想以某种方式使此输入等于我拥有并工作的列表的名称(列表实际上更大,我将它们剪了下来,以便于阅读)

The code is here: 代码在这里:

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")

The problem lies in that you are trying to use Select_Unit as the list rather than the name of the list you want to access. 问题在于,您试图将Select_Unit用作列表而不是要访问的列表的名称

There are a few different ways to implement this, and you should experiment with different ways if you want to push your Python knowledge further. 有几种不同的方法可以实现此目的,并且如果您想进一步推广Python知识,则应该尝试不同的方法。

A way that comes to mind off the bat would be to store the units in a Python dictionary of lists rather than a series of lists, this would allow you to immediately access the value (the unit) behind a unit name once you have verified that it exists in your units. 想到的一种方法是将单位存储在Python列表字典中,而不是一系列列表中,这将使您在验证以下内容后立即访问单元名称后面的值(单元)它存在于您的单位中。

It might look something like the following 它可能看起来像以下内容

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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM