我正在尝试验证用户输入的值,用户输入的值将以一种方法存储,而其值验证则以另一种方法进行。但是我面临的问题是如何从一种方法到另一种方法使用变量值。我们如何在python中执行此操作?我收到错误消息-“未定义userDetailsValidation”这是我的代码:
from userAccountDatabase import *
def userInputs():
userDetails=[]
firstName=str(input("Enter First Name").upper())
while True:
if(userDetailsValidation(firstName, "FirstName", accountDetails))==True:
userDetails.append(firstName)
break
else:
print('Enter valid First Name')
firstName=str(input("Enter First Name").upper())
LastName=str(input("Enter Last Name").upper())
while True:
if(userDetailsValidation(LastName, "LastName", accountDetails))==True:
userDetails.append(LastName)
break
else:
print('Enter valid last Name')
firstName=str(input("Enter Last Name").upper())
dateOfBirth=str(input("Enter date of birth"))
while True:
if(userDetailsValidation(dateOfBirth, "LastName", accountDetails))==True:
userDetails.append(dateOfBirth)
break
else:
print('Enter valid dateOfBirth ')
firstName=str(input("Enter dateOfBirth").upper())
# return firstName,LastName,dateOfBirth
userInputs()
def userDetailsValidation(value, fieldName, database):
print("The account does not exist with the given details,Enter valid First name")
for entry in database:
if fieldName in entry and entry[fieldName] == value:
print("correct value")
return True
else:
return False
def printRequiredUserInfo(FirstName,fieldname,AccountNumber,Accountbalance,Database):
for entry in Database:
if fieldname in entry and entry[fieldname] == FirstName:
print(entry)
printRequiredUserInfo(firstName,"FirstName","Account Number","Account Balance",accountDetails)
答案 0 :(得分:1)
在if(userDetailsValidation(dateOfBirth, "LastName", accountDetails))==True:
中调用您的userDetailsValidation函数仍然无法解析
您可以在userInputs()之前移动userDetailsValidation()的实现,或者在定义userDetailsValidation()之后将调用移至userInputs()。
关于在另一个函数中使用变量值的问题,您有多种选择。