0
# -*- coding: utf-8 -*-

class NameCard:
    def __init__(self, name, age, IG, phone):
        self.name = name
        self.age = age
        self.IG = IG
        self.phone = phone

    def __str__(self):
        return "name: {}, age: {}, IG: {}, phone: {}"\
                .format(self.name, self.IG, self.age,
                       self.phone)

def get_menu():
    while True:
        try:
            menu = int(input("menu: "))
            return menu
        except:
            print("You entered the menu incorrectly. Please re-enter.")

def get_namecard_info():
    name = input("name: ")
    age = input("age: ")
    IG = input("IG: ")
    phone = input("phone: ")
    card = NameCard(name, age, IG, phone)
    return card

def find_namecard(namecard_list, phone):
    for i, card in enumerate(namecard_list):
        if phone==card.phone:
            return i
    return -1

namecard_list = []

while True:
    print("INSERT(1), SEARCH(2), MODIFY(3), DELETE(4), EXIT(0)")
    menu = get_menu()
    if menu==1:
        new_card = get_namecard_info()
        namecard_list.append(new_card)
    elif menu==2:
        find_phone = input("The phone number of the person you are looking for : ")
        index = find_namecard(namecard_list, find_phone)
        if index>=0:
            print(namecard_list[index])
        else:
            print("No data found.")
    elif menu==3:
        print("Select menu number 3")
    elif menu==4:
        print("Select menu number 4")
    elif menu==5:
        print("Select menu number 5")
        for card in namecard_list:
            print(card)
    elif menu > 5:
        print("You entered the menu incorrectly. Please re-enter.")
    elif menu==0:
        break

** If I enter a name, I get this error. **

enter image description here

** But when I input a number, it works normally. ** enter image description here

It's probably a data type problem, but I don't know which part is wrong. As far as I know, python does not have to declare the data type of a variable when receiving input, which is the difference from the C language. Does it change when using a class or function or when using the format function?

What code do I need to modify to make it work?

이재하
  • 1
  • 5

2 Answers2

0

What version of Python are you using?

This seems to be happening for versions prior to 3 in which input has a different behavior: input() error - NameError: name '...' is not defined

You could try with name = raw_input("name: ") instead as pointed out in the answer.

Uretki
  • 197
  • 9
0

I tested your script and it's working fine in my system as well as I checked the code that also seems OK . Here is the output

enter image description here

I have tested it in VS Code using Python 3.8.5 in the 64-bit system.

So maybe the problem is in the version of Python because before Python3 there was a notion of raw_input.

Let me know your Python version.

Lakshika Parihar
  • 1,017
  • 9
  • 19