You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python酒店预订程序信用卡验证逻辑错误排查求助

问题排查与修复

核心错误原因

信用卡验证失败的根源是卡片数据的存储格式与验证逻辑不匹配:

  • 使用pd.read_csv(...).to_dict()时,默认生成的是列导向的字典(键为列名,值是包含行索引和对应值的嵌套字典),结构类似:
    {"number": {0: "1234", 1: "5678"}, "expiration": {0: "12/26", 1: "12/28"}, ...}
    
  • 你的验证逻辑是把用户提交的卡片信息转成frozenset后,判断是否存在于这个列导向字典中,这完全不符合数据结构,导致永远返回False。

修复步骤

1. 修正卡片数据的读取方式

将卡片数据转换为行导向的字典列表,每个元素对应一张信用卡的完整信息:

df_cards = pd.read_csv("cards.csv", dtype=str).to_dict('records')

转换后的数据结构为:

[
  {"number": "1234", "expiration": "12/26", "cvc": "123", "holder": "JOHN SMITH"},
  {"number": "5678", "expiration": "12/28", "cvc": "456", "holder": "JANE SMITH"}
]

2. 简化信用卡验证逻辑

直接对比用户提交的卡片信息字典是否存在于上述列表中,同时加入大小写兼容处理(避免持卡人姓名大小写输入错误):

def validate(self, expiration, holder, cvc):
    card_data = {
        "number": self.number,
        "expiration": expiration,
        "holder": holder.upper(),
        "cvc": cvc
    }
    return card_data in df_cards

修改后的完整代码

import pandas as pd

df = pd.read_csv("hotels.csv", dtype={"id": str})
# 修正卡片数据读取方式
df_cards = pd.read_csv("cards.csv", dtype=str).to_dict('records')


class Hotel:
    def __init__(self, hotel_id):
        self.hotel_id = hotel_id
        self.name = df.loc[df["id"] == self.hotel_id, "name"].squeeze()

    def book(self):
        """book a hotel by changing its availability to no"""
        df.loc[df["id"] == self.hotel_id, "available"] = "no"
        df.to_csv("hotels.csv", index=False)

    def available(self):
        """check if the hotel is available"""
        availability = df.loc[df["id"] == self.hotel_id, "available"].squeeze()
        return availability == "yes"


class ReservationTicket:
    def __init__(self, customer_name, hotel_object):
        self.customer_name = customer_name
        self.hotel = hotel_object

    def generate(self):
        content = f"""
         Thank you for your reservation!
         Here are your booking data
         Name: {self.customer_name}
         Hotel name : {self.hotel.name}
         """
        return content


class CreditCard:
    def __init__(self, number):
        self.number = number

    def validate(self, expiration, holder, cvc):
        card_data = {
            "number": self.number,
            "expiration": expiration,
            "holder": holder.upper(),
            "cvc": cvc
        }
        return card_data in df_cards


print(df)
hotel_ID = input("Enter the Id of the hotel :")
hotel = Hotel(hotel_ID)

if hotel.available():
    # 改为用户输入信用卡信息,符合程序需求
    card_number = input("Enter your credit card number: ")
    expiration = input("Enter expiration date (MM/YY): ")
    holder = input("Enter card holder name: ")
    cvc = input("Enter CVC: ")
    
    credit_card = CreditCard(number=card_number)
    if credit_card.validate(expiration=expiration, holder=holder, cvc=cvc):
        hotel.book()
        name = input("Enter your name: ")
        reservation_ticket = ReservationTicket(customer_name=name, hotel_object=hotel)
        print(reservation_ticket.generate())
    else:
        print("there was a problem with your payment")
else:
    print("Hotel is not free")

内容的提问来源于stack exchange,提问作者user2227531

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 11:07:26