CRUD程序,用于在txt文件中读取,写入和附加详细信息

时间:2019-06-05 08:48:39

标签: python-3.x

这是一个学校作业,一个事件管理系统。它将数据写入txt文件并检索。它主要是CRUD程序,但不确定为什么它没有运行。它在VS CODE IDE上将空间显示为错误。

它将创建一个客户,要求他要预订的座位。他还可以在24小时之前根据参考号删除席位。


import random
print("Welcome to the Event System")


def menu():
    print("Choose the number from menu")
    print("1:Create Customer")
    print("2:Reserve a seat")
    print("3.Cancel the seat")
    print("4.Exit the Event System")
    option = input("put down the number")
return option

def executingmenuinputchoice(option):
    if(option == 1):
        createcust()
    elif(option == 2):
        reserveseat()
    elif(option == 3):
        cancelseat()
    elif(option == 4):
        exit()
    else:
        print('you have chose a wrong option')
        menu()


def createcust():
    print('Provide Customer details')
    name = input('Full name? --> ')
    pno = input('phone number? -> ')
    email = input('Email Id? --> ')
    try:
        file = open("cust.txt", "r")
        lines = file.read().splitlines()
        last_lines = lines[-1]
        content = last_lines.split()
        refno = random.randint(10001, 99999)
        file.close()
        file = open("cust.txt", "a")
        file.write("\n"+" "+name+" "+pno+" "+email+" "+refno)
        file.close()
        print(refno + 'is your reference number')
        print('Added Customer to file')
    except IOError:
        print("File doesn't exist at location")
    except TypeError:
        print('input proper data')
    return createcust()


def customerexist(refno):
    try:
       file = open("cust.txt", "r")
       for line in file:
            content = line.split()
            if (refno == int(content[4])):
                file.close()
                return True,int(content[5])
    except IOError:
            print("File doesn't exist")
            file.close()
    return False,0

def reserveseat():
    referencenumber=input("Enter the Reference Number-->")
    refexist =referenceexist(referencenumber)
    if(refexist==True):
            seatsyouwantbook=input("Number of seats you want to book? ->")
            date=datetime.datetime.now()
            seats=seats+seatsyouwantbook
            newline=""
    try:
        file=open("cust.txt","r")
        lines=file.read().splitlines()
        last_linesno=len(lines)
        currentLine=1
        for line in lines:
            content=line.split
            if(currentline!=last_linesno):
                if(refno==int(content[4])):
                    file.close()
                    return True,int(content[5])
    except IOError:
            print("FIle never existed")
            file.close()
    return False,0

def cancelseat():
    try:
        file=open("cust.txt","r")
        for line in file:
            content=line.split()
            if (refno==int(content[4])):
                file.close()
                return True,int(content[5])
    except IOError:
        print("File doesn't exist")
        file.close()
        return False,0
invalid syntax (<unknown>, line 41)

我希望它能正常运行,所以我可以再次提交它。

1 个答案:

答案 0 :(得分:0)

我没有检查您的整个代码,但至少可以运行它,以便进一步纠正它:-

import random
print("Welcome to the Event System")

def customerexist(refno):
    try:
       file = open("cust.txt", "r")
       for line in file:
            content = line.split()
            if (refno == int(content[4])):
                file.close()
                return True,int(content[5])
    except IOError:
            print("File doesn't exist")
            file.close()
    return False,0

def reserveseat():
    referencenumber=input("Enter the Reference Number-->")
    refexist =referenceexist(referencenumber)
    if(refexist==True):
            seatsyouwantbook=input("Number of seats you want to book? ->")
            date=datetime.datetime.now()
            seats=seats+seatsyouwantbook
            newline=""
    try:
        file=open("cust.txt","r")
        lines=file.read().splitlines()
        last_linesno=len(lines)
        currentLine=1
        for line in lines:
            content=line.split
            if(currentline!=last_linesno):
                if(refno==int(content[4])):
                    file.close()
                    return True,int(content[5])
    except IOError:
            print("FIle never existed")
            file.close()
    return False,0

def cancelseat():
    try:
        file = open("cust.txt","r")
        for line in file:
            content=line.split()
            if (refno==int(content[4])):
                file.close()
                return True,int(content[5])
    except IOError:
        print("File doesn't exist")
        file.close()
        return False,0

def createcust():
    print('Provide Customer details')
    name = input('Full name? --> ')
    pno = input('phone number? -> ')
    email = input('Email Id? --> ')
    try:
        file = open("cust.txt", "r")
        lines = file.read().splitlines()
        last_lines = lines[-1]
        content = last_lines.split()
        refno = random.randint(10001, 99999)
        file.close()
        file = open("cust.txt", "a")
        file.write("\n"+" "+name+" "+pno+" "+email+" "+refno)
        file.close()
        print(refno + 'is your reference number')
        print('Added Customer to file')
    except IOError:
        print("File doesn't exist at location")
    except TypeError:
        print('input proper data')
    return createcust()

def menu():
    print("Choose the number from menu")
    print("1:Create Customer")
    print("2:Reserve a seat")
    print("3.Cancel the seat")
    print("4.Exit the Event System")
    option = input("put down the number")
    return option

def executingmenuinputchoice(option):

    option = int(option)
    if(option == 1):
        createcust()
    elif(option == 2):
        reserveseat()
    elif(option == 3):
        cancelseat()
    elif(option == 4):
        exit()
    else:
        print('you have chose a wrong option')
        menu()


executingmenuinputchoice(menu())

错误原因:-

  • 您的缩进处处都是python语言 优先考虑缩进,因为它使用它来确定块跨度,因此您应该 在整个代码中保持一致的缩进方案。
  • Python使用自上而下的方法来解释〜 程序,即它只跟踪它已经存在的内容 遇到。在您的代码中,函数executingmeninputchoice()menu()(用于UI的两个主要功能)堆叠在一起 高于所有其他功能,因此当您尝试调用其他功能时 这两个函数中的函数,它们没有被调用。作为程序 不知道这些功能是否存在(因为它不存在) 遇到他们了
  • 函数中存在逻辑错误 您尝试加入时的executingmenuinputchoice(option) 输入一个字符串并将其与整数值进行比较,然后 因此,每当操作失败并控制权转移时 到else块,因此每次您获得相同的输出 'you have chose a wrong option'不管输入是否为 是否合法

PS:-我尚未测试您的完整代码,因为这不是我的服务代码,因此可能还会存在其他逻辑错误,因此建议您查找并修复这些错误太。

相关问题