如何在此程序中实现功能以验证或使帐号无效?

时间:2017-07-19 06:15:47

标签: python python-2.7

这是我的代码到目前为止。名为current_accts的列表应验证所有帐户。验证输入的帐号应在名为check_account()的函数中完成,该函数将接受用户输入的帐户以及列表current_accts。如果帐户有效,此函数应返回1,否则如果帐户无效则返回0。任何人都可以帮我加入这些功能吗?

accNum = int(input("Please enter your seven digit account number: "))   
if accNum == 5679035:
     print 'Account', accNum,'is valid'
elif accNum ==  8232322:
     print 'Account', accNum,'is valid'
elif accNum == 2134988:
     print 'Account', accNum,'is valid'
elif accNum == 6541234:
     print 'Account', accNum,'is valid'
elif accNum == 3984591:
     print 'Account', accNum,'is valid'
elif accNum == 1298345:
     print 'Account', accNum, 'is valid'
elif accNum == 7849123:
     print'Account', accNum, 'is valid'
elif accNum == 8723217:
     print'Account', accNum, 'is valid'
else:
     print "Account", accNum, "is NOT valid"

3 个答案:

答案 0 :(得分:2)

在python中实现它非常简单。

valid_account_list = [1,2,3,4]
input_account = 4 # user input
def validate_account(input_account ,valid_account_list)
    if input_account in valid_account_list:
        return 1
    else:
        return 0

答案 1 :(得分:1)

Pythonic方式

def validate_account(input_account, valid_account_list):
    return 1 if input_account in valid_account_list else 0

答案 2 :(得分:0)

这是您想要的代码示例:

try:
  def check_account(accNum):
     current_acts=[5679035,8232322,2134988,6541234,3984591,1298345,7849123,8723217]
     return accNum in current_acts

  accNum = int(input("Please enter your seven digit account number: "))
  is_valid_account = check_account(accNum)
  if is_valid_account:
     print 'The digit account number ' + str(accNum) + ' is valid.'
  else:
     print 'The digit accoutn number ' + str(accNum) + ' is not valid.'


except ValueError:
   #the input is not a valid number
   print "You should add a vaild digit account number"