【问题标题】:Issues with lists? Error checking not working列表有问题?错误检查不起作用
【发布时间】:2016-12-17 14:21:44
【问题描述】:

我对 python 比较陌生,我刚刚开始学习如何使用类。这是我尝试整合它们的第一个程序,但我想出了一个我似乎无法解决的小问题,我认为它与列表有关。代码如下: (主题是让用户选择要购买的座位类型)。

class SeatBooking:
    def __init__(self, seat):
        self.seat = seat
        possible_types = []
        possible_types.extend(["Low_Economy", "Standard_Economy", "High_Economy",
                        "Business", "First", "Residence"])
        possible_types = " ".join(possible_types)
        while True:
            if self.seat not in possible_types:
                print("Sorry, but this is not a valid answer. Please try again!")
                self.seat = str(input("What type of ticket would you like? The possible types are: {} "
                            .format(possible_types)))
            else:
                print("You have chosen to book a {} ticket.".format(self.seat))
                confirmation = str(input("Please confirm with 'Yes' or 'No': ")).lower()
                if confirmation == "yes":
                    print("Excellent decision! Ready to continue")
                    print("=" * 170)
                    break
                elif confirmation == "no":
                    self.seat = str(input("What type of ticket would you like? The possible types are: {} "
                                .format(possible_types)))
                else:
                    print("That doesn't seem to be a valid answer.")

这是主文件(用于执行我将创建的不同类):

import type_seat
# Choose the seat to book
print("=" * 170)
print("Welcome to Etihad! This program can help you organize your       flight, payments and usage of miles!")
possible_types = []
possible_types.extend(["Low_Economy", "Standard_Economy", "High_Economy",
                   "Business", "First", "Residence"])
possible_types = " ".join(possible_types)
seat_type = str(input("What type of ticket would you like? The possible     types are: {}. "
                      .format(possible_types)))

type_seat.SeatBooking(seat_type)

我遇到的问题是我似乎可以输入某些字母,即使它们不是可用的座位之一,也不会将它们视为错误。例如,当我输入字母“h”或“s”时,我的错误检查代码部分不会响应它,但是当我输入字母“b”或像“try”这样的随机词时它会响应。不过,它似乎并不是完全随机的,而且它似乎只发生在 possible_types[] 列表中的前 3 个“项目”的字母或部分中。但是,我还没有完全测试这一点。这就是为什么我认为它与列表有关,所以如果有人知道是什么原因造成的,如果他们能帮助我解决这个问题,或许能帮助我在未来避免重复这个错误,我将不胜感激!

注意,对于我正在使用.join 的列表,但我也尝试了 str()。

【问题讨论】:

  • h 在你的possible_types中;那只是一个包含一系列字符的字符串,而字符 h 是其中的一部分(在单词 High_Economy 中)。序列try 不是。为什么你使用" ".join() 来创建这个字符串而不是列表?
  • 天哪,你是对的!我删除了它并且它起作用了。我认为我必须在 str() 之前使用 .join ,因为我一直遇到问题,但我猜不是。另外,我不是最好的;)。无论如何,一个问题。你知道为什么这行: self.seat = str(input("What type of ticket you like? The possible types are: {} " .format(possible_types))) 给我一个键盘中断?
  • 1) 你不需要调用 str(),在 Python 3 中 input() 总是 返回一个 strinput() 需要一个支持用户输入响应的控制台;我猜您可能是在 IDE 控制台或类似的没有输入的地方运行此代码?这可能会导致 EOF 被引发或键盘中断。否则,不知道。
  • 等待新版本由于某种原因似乎不起作用..没关系。当我尝试输入某些内容时,没有任何效果。
  • 我正在使用 Pycharm

标签: python list python-3.x


【解决方案1】:

您没有列表,您正在针对一个长字符串测试字符:

possible_types = " ".join(possible_types)

字母hs 在该字符串中(分别在单词High_EconomyBusiness 中),但序列try 不出现在字符串中的任何位置。

如果您只想让整个单词匹配,您需要将possbile_types 留下一个列表,或者最好将其转换为一个集合(因为集合允许快速成员资格测试)。你可以定义列表,这里不需要list.extend()

possible_types = ["Low_Economy", "Standard_Economy", "High_Economy",
                   "Business", "First", "Residence"]

或使用{...} 将其设置为一组:

possible_types = {"Low_Economy", "Standard_Economy", "High_Economy",
                   "Business", "First", "Residence"}

不要不要把它加入一个字符串,直接针对对象进行测试:

if self.seat not in possible_types:

如果您仍需要在错误消息中向用户显示这些值,请加入这些值然后,或为此目的将str.join() 结果存储在不同的变量中。

请注意,您不应在 __init__ 类方法中处理用户输入验证。将用户交互留给单独的一段代码,并在您验证后创建您的类的实例。这样您就可以轻松更换用户界面,而无需调整所有数据对象。

【讨论】:

  • 我编辑了我的帖子。如果可能的话,你能读一下我说的话并提供帮助吗?谢谢。还是一头雾水。然而,你的回答让我重回正轨。我不会再在课堂上使用输入法了。谢谢!
  • @Eric:请使用新问题解决后续问题,我回滚了您的编辑。也就是说,您使用的是str.lower(),但您的列表包含带有大写和小写字母的字符串。 'abc' in ['Abc', 'aBc', 'abC'] 为假,因为该列表中没有 'abc'(全部小写)..
  • 我还是不明白。我在用户输入后使用 .lower() 只是为了不区分大小写。但是,现在我想我在这方面已经过头了。在我开始尝试编码之前,我最好先更详细地学习这个主题!我认为这件小事不值得提出新问题,但感谢您的所有帮助。给出的最佳答案:)。
【解决方案2】:
possible_types = " ".join(possible_types)

上述语句将创建一个字符串“Low_Economy Standard_Economy High_Economy Business First Residence”。

现在你正在做

if self.seat not in possible_types:

这将检查字符串中是否存在特定字符。在您的情况下,您会找到存在的“h”和不存在的“尝试”。

如果您删除此语句,您的程序将运行

possible_types = " ".join(possible_types)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-04
    • 1970-01-01
    • 2021-03-02
    相关资源
    最近更新 更多