需要帮助在python 2.7中写if if语句atm以获得引脚保护

时间:2016-03-13 17:29:16

标签: python python-2.7

我试图为我的ATM创建一个针保护的东西,你可以输入一个代码等。然后它要求你,但我想要它,所以如果你输入错误的密码,它说不正确再试一次,你不能继续进入主程序。也许可能有一定的尝试?如果我想保存东西,我也不必写文件?但除非你知道如何在我的代码中使用它,否则永远不要明白....

balance = float(0)

userInput = None

print("Hello, Welcome to the ATM")
print("")
print("Please begin with creating an account")
name = raw_input("Enter your name: ")
code = raw_input("Please enter a 4 digit pin to use as your passcode: ")

code = int(input('Please enter the 4 digit pin on your card:'))
if code == (code): print("correct pin!")       

print "Hello , welcome to the ATM"

while userInput != "4":
    userInput = raw_input("\n what would you like to do?\n\n     (1)Check balance\n     (2)Insert funds\n" +
    "     (3)Withdraw funds\n     (4)Exit the ATM\n" )

    if userInput == "1":
        print "your balance is", "£" , balance

    elif userInput == "2":
        funds = float(raw_input("Enter how much money you want to add"))
        balance = balance + funds

    elif userInput == "3":
        withdraw = float(raw_input("Enter how much money you want to withdraw..."))
        balance = balance - withdraw

    elif userInput == "4":
        print "Thanks for using the ATM!"

1 个答案:

答案 0 :(得分:0)

balance = float(0)

userInput = None

print("Hello, Welcome to the ATM")
print("")
print("Please begin with creating an account")

name = raw_input("Enter your name: ")

# take the code as a string
saved_code = str(raw_input("Please enter a 4 digit pin to use as your passcode: "))

#validate that the code is a number and is 4 digit
try:
    int(saved_code)
    if len(saved_code)!=4:
        raise
except Exception, e:
    print("Error: Pin is not a valid 4 digit code")
    exit()

# set trails and trail counter 
totalTrails = 3;
currentTrail = 0;
status = 1;

# take input from user and compare codes
while currentTrail < totalTrails: 
    user_code =str(raw_input('Please enter the 4 digit pin on your card:'))
    if user_code==saved_code:
        status=0
        break;
    else:
        currentTrail+=1

if status==0:
    print("correct pin!")
else:
    print("You tried to enter a wrong code more than three times.")
    exit();       

print "Hello , welcome to the ATM"

while userInput != "4":
    userInput = raw_input("\n what would you like to do?\n\n     (1)Check balance\n     (2)Insert funds\n" +
    "     (3)Withdraw funds\n     (4)Exit the ATM\n" )

    if userInput == "1":
        print "your balance is", "$" , balance

    elif userInput == "2":
        funds = float(raw_input("Enter how much money you want to add"))
        balance = balance + funds

    elif userInput == "3":
        withdraw = float(raw_input("Enter how much money you want to withdraw..."))
        balance = balance - withdraw

    elif userInput == "4":
        print "Thanks for using the ATM!"