Home > front end >  Return statement not executed when valid member number is entered
Return statement not executed when valid member number is entered

Time:10-27

Morning all,

So I've tried to create an account class with a scan method that takes a "fictitious" scanning of a members card, however, when I enter a valid card_number in the input field it returns another input field, yet if I enter an invalid card_number it returns Invalid ID

Here is my code, where am I going wrong please?

from enum import Enum
from abc import ABC, abstractmethod
import csv
import json
from datetime import datetime, timedelta, date

membersJsonPath = 'members.json'

class BookStatus(Enum):
  AVAILABLE, RESERVED, LOANED, LOST = 1, 2, 3, 4

class ReservationStatus(Enum):
  WAITING, PENDING, CANCELED, NONE = 1, 2, 3, 4

class AccountStatus(Enum):
  ACTIVE, CLOSED, CANCELED, NONE = 1, 2, 3, 4

class Account(ABC):
    def __init__(self, status=AccountStatus.ACTIVE):
        self.__id = self.scan_id()
        self.__status = status
        
    def scan_id(self):
      with open(membersJsonPath, 'r') as membersJson:
        data = json.load(membersJson)
        input_id = input("Please enter your member ID: ")
        for member in data['members']:
          if member['card_number'] == input_id:
            return ("Welcome, "   member['first_name']   "!") 
          else:
            return "Invalid ID"

a = Account()
a.scan_id()

Here is a portion of the json file that I created from a CSV file

{
    "members": [
        {
            "\u00ef\u00bb\u00bfid": "1",
            "first_name": "Adelaide",
            "last_name": "Cunningham",
            "gender": "Female",
            "email": "[email protected]",
            "card_number": "13"
        },
        {
            "\u00ef\u00bb\u00bfid": "2",
            "first_name": "Charlie",
            "last_name": "Roberts",
            "gender": "Male",
            "email": "[email protected]",
            "card_number": "22"
        },
        {
            "\u00ef\u00bb\u00bfid": "3",
            "first_name": "Eric",
            "last_name": "Cooper",
            "gender": "Male",
            "email": "[email protected]",
            "card_number": "33"
        },
        {
            "\u00ef\u00bb\u00bfid": "4",
            "first_name": "Cadie",
            "last_name": "Hall",
            "gender": "Female",
            "email": "[email protected]",
            "card_number": "43"
        }
   ]
}

CodePudding user response:

The problem is in this for loop:

for member in data['members']:
    if member['card_number'] == input_id:
        return ("Welcome, "   member['first_name']   "!") 
    else:
        return "Invalid ID"

Here, if the input_id is equal to the first member's card number, it returns the welcome message, otherwise "Invalid ID". You want to scan all card numbers, and only return "Invalid ID" if none of them match, so the second return statement should be after the end of the for loop:


for member in data['members']:
    if member['card_number'] == input_id:
        return ("Welcome, "   member['first_name']   "!") 
    
return "Invalid ID"

CodePudding user response:

You are snanning id by create object.

self.__id = self.scan_id()

It's correct behavior?

  • Related