【发布时间】:2021-10-02 07:03:43
【问题描述】:
# -*- 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
** 如果我输入名称,我会收到此错误。 **
** 但是当我输入一个数字时,它可以正常工作。 ** enter image description here
可能是数据类型的问题,但不知道是哪一部分出错了。据我所知,python在接收输入时不必声明变量的数据类型,这是与C语言的区别。使用类或函数或使用格式函数时会发生变化吗?
我需要修改哪些代码才能使其正常工作?
【问题讨论】:
-
对我有用吗?
-
什么意思?
标签: python string input output crud