Python 3.6-文件将不起作用,出现语法错误

时间:2018-10-12 09:40:35

标签: python python-3.6 python-3.7

上次我问a question

我的学校有一个过时的Python版本,文件的一部分显示“语法无效”。
图片在这里:

Error file

这是我修改过的程序:

create = ""
import time
import sys
import os

ACCOUNTS_FILE = r'H:/Dice game/NO/How about no/accounts.txt'
if not os.path.exists(ACCOUNTS_FILE):
    with open(ACCOUNTS_FILE, 'w') as f:#
        pass

MINIMUM_NAME = 5
MINIMUM_PASS = 8
def delete_account():
    account_name = input_account_name()
    password = input_password()

    accounts = get_accounts()
    if any(f'{account_name}:{password}' == account.strip() for account in accounts):#
        if not confirm_deletion():
            print('Nothing was deleted.')
            return

        with open(ACCOUNTS_FILE, 'w') as accounts_file:
            accounts = (a for a in accounts if a.strip() != f'{account_name}:{password}')#
            accounts_file.writelines(accounts)
        print('Account deleted.')
    else:
        print('Account unknown or wrong password')

def input_account_name():
    account_name = ''
    while len(account_name) < MINIMUM_NAME:
        account_name = input('Please enter the account name '
                             f'(at least {MINIMUM_NAME} characters)> ')#
    return account_name

def input_password():
    password = ''
    while len(password) < MINIMUM_PASS:
        password = input('Please enter a password for the account '
                         f'(at least {MINIMUM_PASS} characters)> ')#
    return password

def get_accounts():
    with open(ACCOUNTS_FILE, 'r') as accounts_file:
        accounts = accounts_file.readlines()
    return accounts

def confirm_deletion():
    confirmation = ''
    while confirmation not in ['y', 'yes', 'n', 'no']:
        confirmation = input('Are you sure you want to delete this account?\n'
                             'You will lose all your data – (Y)es or (N)o?> ').lower()
    return confirmation in ['y', 'yes']

acc = ''
while acc != 'quit':
    print("Type create to create, delete to delete")
    acc = input("an account or quit to exit Python: ")
    if acc.lower() == "create":
        found = False
        print("Please enter your name.")
        create = input(">").lower()

        if len(create) < MINIMUM_NAME:
            print("The name is too short.")
            print("Please enter your name.")
            create = input(">").lower()


        # Open a file
        fi = open("H:/Dice game/NO/How about no/accounts.txt", "r")    
        #get the data
        data = fi.readlines()   
        # Close opened file
        fi.close()


        #check each line in the text file
        for li in data:
            #if the name is already in a file
            if(li.split(":")[0].lower() == create):
                #print an error of similar username
                print("This name is already taken. Try a different one.")
                #the name has been found
                found = True


        #if the name wasn't found anywhere in the file
        while found == False:
        #ask the user for the password
            passk = input("Please type a new password:\n>")
            if len(passk) < MINIMUM_PASS:
                print("Password must be a minimum of 8 letters.")
                passk = input(">")
                # Open a file to append to
            fi = open("H:/Dice game/NO/How about no/accounts.txt", "a")    
                #add the new name/password to the file
            fi.write("\n" + create + ":" + passk)

            # Close opened file
            fi.close()
            print("Created an account. You may now login.")
            break

    #Deleting an account
    elif acc.lower() == 'delete':
        delete_account()
        pass

    elif acc.lower() != 'quit':
        time.sleep(.1)

错误会以#结尾突出显示。我在笔记本电脑上使用的是Python的较新版本3.7,但在学校中该版本可能是3.6,因此我需要一种方法来编写代码

if any(f'{account_name}:{password}' == account.strip() for account in accounts): 

工作无间断。

0 个答案:

没有答案