[請益] APP 註冊用戶姓名重複
我在編寫一個名為**ve**e* App的程序
應用程序用於管理用戶信息
每個用戶都有一個唯一的用戶ID和姓名
用戶可以登錄、註冊和修改個人信息
我已完成並進行一些測試
然後在最後的測試中,我發現一個問題
當用戶嘗試修改姓名時,有時會發生姓名重複的情況
即兩個不同的用戶的姓名,變成了相同的
現在產生問題是
有時候可以正確檢測到重複,有時候就會漏掉
是否我函數部份搞錯(? 還是.....
新手努力摸索還在寫碼測試~請多包涵
******e* App
def __init__(self):
self.users = {}
def register_user(self, user_id, name):
if user_id in self.users:
print("User ID already exists. Please register with a different ID
.")
else:
self.users[user_id] = name
print(f"User {user_id} registered successfully with the name {name
}.")
def login_user(self, user_id):
if user_id in self.users:
print(f"Welcome! User {user_id} is logged in with the name {self.u
sers[user_id]}.")
else:
print("User ID does not exist. Please register as a new user.")
def modify_name(self, user_id, new_name):
if user_id in self.users:
current_name = self.users[user_id]
if current_name == new_name:
print("New name is the same as the current name. Please enter
a different name.")
else:
for _, name in self.users.items():
if name == new_name:
print("Name already exists. Please enter a different n
ame.")
break
else:
self.users[user_id] = new_name
print(f"User {user_id}'s name has been modified to {new_na
me}.")
else:
print("User ID does not exist. Please register as a new user.")
if __name__ == "__main__":
app = VeeApp()
app.register_user("001", "Rosa")
app.register_user("002", "Calii")
app.login_user("001")
app.login_user("003")
app.modify_name("001", "Calii")
app.modify_name("002", "Rosa")
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 42.72.2.145 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/MacDev/M.1690700652.A.D59.html
※ 編輯: cici950630 (42.72.2.145 臺灣), 07/30/2023 15:14:15
推
07/30 16:37,
1年前
, 1F
07/30 16:37, 1F
→
07/30 16:38,
1年前
, 2F
07/30 16:38, 2F
→
07/30 16:43,
1年前
, 3F
07/30 16:43, 3F
→
07/30 16:44,
1年前
, 4F
07/30 16:44, 4F
→
07/30 16:44,
1年前
, 5F
07/30 16:44, 5F
→
07/30 16:45,
1年前
, 6F
07/30 16:45, 6F
→
07/30 17:55,
1年前
, 7F
07/30 17:55, 7F
※ 編輯: cici950630 (42.72.2.145 臺灣), 07/30/2023 17:57:16
噓
11/28 08:36, , 8F
11/28 08:36, 8F
→
11/28 08:36, , 9F
11/28 08:36, 9F
MacDev 近期熱門文章
PTT數位生活區 即時熱門文章