首页 > 解决方案 > 不同功能的结果以制作收据

问题描述

我需要帮助打印我的程序的收据,所以基本上,用户将他们的个人详细信息输入程序,我尝试使用以下功能使验证完美,但问题是当我使用这些功能进行验证时,我不知道如何从功能获取正确的信息到我的收据。我不知道如何将不同功能的所有结果放入一张收据中,就像我在底部的格式一样。当打印收据时,它说(日期:无)(电子邮件:无)(电话:无)但我尝试过许多不同方式的收据中的实际输入不起作用。

def date():
    inputDate = input("Enter the booking date in format 'dd/mm/yy' : ")

    day, month, year = inputDate.split('/')

    isValidDate = True
    try:
        datetime.datetime(int(year), int(month), int(day))
    except ValueError:
        isValidDate = False

    if(isValidDate):
        print("Input date is valid ..")
    else:
        print("Input date is not valid..")
        date()


def validateEmail(email):
    return re.match(r'[\w-]{1,20}@\w{2,20}\.\w{2,3}$', email)



def checkemail():
    email = console.input('[bright_white]Please enter your email address: [/]')
    valid = validateEmail(email)
    if valid:
        print(email, 'is valid')
    else:
        print("invalid email format", email)
        checkemail()

def phone():
    phoneno = console.input('[bright_white]Please enter your phone number :[/] ')
    patter = r"^(1|8|9)[0-9]{7}"

    if re.match(patter, phoneno) and len(phoneno) == 8:
        print("Valid")
    else:
        print("Invalid")
        phone()


def booking_form():

    bookings = []


    FirstName = input('Please enter your first name: ')
    LastName = input('Please enter your last name: ')
    Email = checkemail()
    ContactNumber = phone()
    Date = date()
    Time = input('Enter time in 00:00 AM/PM format:')
    Gym = input("Please Enter the Name of the Gym Again to Ensure that to be the Correct one:  ")
    Commentsrequest = input('Comments/Additional Request: ')


    bookings.append(FirstName)
    bookings.append(LastName)
    bookings.append(Email)
    bookings.append(ContactNumber)
    bookings.append(Date)
    bookings.append(Time)
    bookings.append(Gym)
    bookings.append(Commentsrequest)


    with open("booking.csv", 'a', newline='') as csvfile:
        writer = csv.writer(csvfile)
        writer.writerow(bookings)


    print(" Date:", date())
    print(" Time:", Time)
    print(" First Name:", FirstName)
    print(" Last Name:", LastName)
    print(" Email:", validateEmail(email))
    print(" Phone Number:", ContactNumber)
    print(" Chosen Location [Area/Gym]:", Gym)
    print(" Additional Notes/Requests:", Commentsrequest)
    print("**********************************************************************", )
    print("|         Thank You for Using the Volleyball Booking System          |", )
    print("|            You have Successfully Booked a Court/Gym                |", )
    print("|             Please Screenshot this Receipt to Keep                 |", )
    print("|                      as Proof of Booking                           |", )
    print("|          Have a Good Day, We will see you on the Court!!           |", )
    print(
        "                          ★★★★★★★★★★★★                              ")
    print("**********************************************************************", )
    exit()

标签: pythonfunctionreceipt

解决方案


您的函数不返回任何内容,因此您无法打印结果,因为它始终是None. 另外,为什么在尝试打印时再次调用这些函数?您不需要这样做,您之前已经保存了它们。这是固定代码:

import re
import datetime
import csv

def date():

    inputDate = input("Enter the booking date in format 'dd/mm/yy' : ")
    day, month, year = inputDate.split('/')
    isValidDate = True
    try:
        datetime.datetime(int(year), int(month), int(day))
    except ValueError:
        isValidDate = False

    if(isValidDate):
        return datetime.datetime(int(year), int(month), int(day))
    else:
        print("Input date is not valid..")
        date()


def validateEmail(email):
    return re.match(r'[\w-]{1,20}@\w{2,20}\.\w{2,3}$', email)



def checkemail():
    email = input('[bright_white]Please enter your email address: [/]')
    valid = validateEmail(email)
    if valid:
        return email
    else:
        print("invalid email format", email)
        checkemail()

def phone():
    phoneno = input('[bright_white]Please enter your phone number :[/] ')
    patter = r"^(1|8|9)[0-9]{7}"

    if re.match(patter, phoneno) and len(phoneno) == 8:
        return phoneno
    else:
        print("Invalid")
        phone()


def booking_form():

    bookings = []


    FirstName = input('Please enter your first name: ')
    LastName = input('Please enter your last name: ')
    Email = checkemail()
    ContactNumber = phone()
    Date = date()
    Time = input('Enter time in 00:00 AM/PM format:')
    Gym = input("Please Enter the Name of the Gym Again to Ensure that to be the Correct one:  ")
    Commentsrequest = input('Comments/Additional Request: ')


    bookings.append(FirstName)
    bookings.append(LastName)
    bookings.append(Email)
    bookings.append(ContactNumber)
    bookings.append(Date)
    bookings.append(Time)
    bookings.append(Gym)
    bookings.append(Commentsrequest)


    with open("booking.csv", 'a', newline='') as csvfile:
        writer = csv.writer(csvfile)
        writer.writerow(bookings)


    print(" Date:", Date)
    print(" Time:", Time)
    print(" First Name:", FirstName)
    print(" Last Name:", LastName)
    print(" Email:", Email)
    print(" Phone Number:", ContactNumber)
    print(" Chosen Location [Area/Gym]:", Gym)
    print(" Additional Notes/Requests:", Commentsrequest)
    print("**********************************************************************", )
    print("|         Thank You for Using the Volleyball Booking System          |", )
    print("|            You have Successfully Booked a Court/Gym                |", )
    print("|             Please Screenshot this Receipt to Keep                 |", )
    print("|                      as Proof of Booking                           |", )
    print("|          Have a Good Day, We will see you on the Court!!           |", )
    print(
        "                          ★★★★★★★★★★★★                              ")
    print("**********************************************************************", )
    exit()


booking_form()

我所做的更改摘要:在date()函数中,我没有打印日期有效,而是返回 了有效的电子邮件,并且在函数上继续循环是日期无效。

我做了同样的事情checkemail(),我已经返回了有效的电子邮件,如果无效,请向用户询问有效的电子邮件。

phone()功能相同。

在文件末尾的打印语句中,我只是使用了Date您之前创建的变量而不是调用date()函数并再次询问用户另一个日期。与电子邮件相同。

现在它工作正常(只要知道你从不检查时间并且输入可能无效)。


推荐阅读