我在运行代码时遇到错误(语法无效)

时间:2016-11-25 11:22:01

标签: python python-3.x

import time
def pini():
    pin1 = int(input("enter your pin please: ")
    if pin1 == pin:
            print("pin correct")
            print("paying: £",amount)
            print("opening connection...")
            time.sleep(0.4)
            print("contacting bank...")
            time.sleep(1.0)
            print("contacting bank...")
            time.sleep(0.5)
            print("contacting bank...")
            time.sleep(0.2)
            print("contacting bank...")
            time.sleep(0.2)
            print("transaction successful")
    elif:
            print("wrong pin, try again")
            pini()

print("Welcome to the bank")
print("\n")
pin = int(input("what is your pin: ")

pay = ""
while pay == "":
    pay = input("press 'n' to make a payement: ").lower()
    if pay == "n":
            amount = int(input("please enter amount to pay: ")
            print("please insert your card to pay" , "£",amount)
            print("\n")
            pini()

这是我的代码。

运行时,我收到“语法无效”错误,我正在使用Python / IDLE 3.4.2。 这只是一个学校项目的小程序,我遇到了错误。

我认为这可能是缩进的问题,但我不确定,任何帮助表示赞赏:)

2 个答案:

答案 0 :(得分:1)

继续Jim说的话,你的代码应该是

def pini():
    pin1 = int(input("enter your pin please: "))

您可能想要考虑使用IDE - https://en.wikipedia.org/wiki/Integrated_development_environment - 因为这些可以突出显示简单的错误(例如Eclipse是我的选择)。询问哪一个是最好的 - What IDE to use for Python?

答案 1 :(得分:1)

我在您的代码中添加了一些注释,如下所示。

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import time
def pini():
    pin1 = int(input("enter your pin please: "))    # miss a right bracket ')'
    if pin1 == pin:                                 # Be attention to your code indentation
        print("pin correct")
        print("paying: £",amount)
        print("opening connection...")
        time.sleep(0.4)
        print("contacting bank...")
        time.sleep(1.0)
        print("contacting bank...")
        time.sleep(0.5)
        print("contacting bank...")
        time.sleep(0.2)
        print("contacting bank...")
        time.sleep(0.2)
        print("transaction successful")
    else:       # it should be `else` instead of `elif`
        print("wrong pin, try again")
        pini()

print("Welcome to the bank")
print("\n")
pin = int(input("what is your pin: "))   # Again, you miss the right bracket `)`

pay = ""

while pay == "":
    pay = input("press 'n' to make a payement: ").lower()
    if pay == "n":
        amount = int(input("please enter amount to pay: ")) # Same problem `)`
        print("please insert your card to pay" , "£",amount)
        print("\n")
        pini()