Python - 打印名称显示无

时间:2016-09-17 14:31:21

标签: python function

import re
import time
import sys

def main():
    name = getName()
    getOption()
    nameForTicket, done = getTraveling(name)
    price, destination = getWay()
    fare = getFare()
    seat = getSeat()
    age = getAge()
    totalcost = getTotalCost(price, fare, seat, age)
    print("\n" + "Thank you " + name + " for flying us!" + "\n" + "The ticket price is: $" + str(totalcost) + "\n" + "The destination is: " + str(destination) + "\n" + "Ticket is for: " + str(nameForTicket).title())
    main2(name, done)




def getName():
    name = input("Welcome to Tropical Airlines! Please enter your name >>> ")
    if not re.match("^[a-zA-Z ]*$", name):
        print("Error, only letters allowed!")
        getName()
    elif len(name) > 15:
        print("Error, Only 15 characters allowed!")
        getName()
    else:
        print ("Welcome " + name.title())
    return name

def getOption():
    print("(I) Information" + "\n" + "(O) Order" + "\n" + "(E) Exit")
    user_choice = input("Choose one of the following option >>> ")
    if user_choice.upper() == "I":
        displayInfo()
    else:
        if user_choice.upper() == "O":
            return
        else:
            if user_choice.upper() == "E":
                print("Thank you for visiting Tropical Airlines")
                exit()
            else:
                print("Error")
                getOption()


def displayInfo():
    print("Thank you for choosing Tropical Airlines for your air travel needs." + "\n" + "You will be asked questions regarding what type of ticket you would like to purchase as well as destination information." + "\n" + "We also offer 50% discounted fares for children.")
    getOption()


def getTraveling(name):
    option = input("Who is the traveling person?" + "\n" + "(Y) You" + "\n" + "(S) Someone else")
    if option.upper() == "Y":
        nameForTicket = name
        done = True
        return nameForTicket, done
    elif option.upper() == "S":
        nameForTicket = getName2()
        done = False
        return nameForTicket, done
    else:
        print("Error")
        getTraveling(name)

def getName2():
    name2 = input("What is the travelling person name?")
    if not re.match("^[a-zA-Z ]*$", name2):
        print("Error, only letters allowed!")
        getName2()
    elif len(name2) > 15:
        print("Error, Only 15 characters allowed!")
        getName2()
    else:
        return name2




def getWay():
    option = input("What kind of trip?" + "\n" + "(1) One way" + "\n" + "(2) Round trip")
    if option == "1":
        cost, destination = getDest1()
        return cost, destination
    elif option == "2":
        cost, destination = getDest2()
        return cost, destination
    else:
        print("Error")
        getWay()

def getDest1():
    option = input("Choose one of the following destination: " + "\n" + "(C) Cairns -- $200" + "\n" + "(P) Perth -- $250" + "\n" + "(S) Sydney -- $300")
    if option.upper() == "C":
        initialCost = 200
        dest = "Cairns"
        return initialCost, dest
    elif option.upper() == "P":
        initialCost = 250
        dest = "Perth"
        return initialCost, dest
    elif option.upper() == "S":
        initialCost = 300
        dest = "Sydney"
        return initialCost, dest
    else:
        print("Error")
        getDest1()


def getDest2():
    option = input("Choose one of the following destination: " + "\n" + "(C) Cairns -- $300" + "\n" + "(P) Perth -- $400" + "\n" + "(S) Sydney -- $500")
    if option.upper() == "C":
        initialCost = 300
        dest = "Cairns"
        return initialCost, dest
    elif option.upper() == "P":
        initialCost = 400
        dest = "Perth"
        return  initialCost, dest
    elif option.upper() == "S":
        initialCost = 500
        dest = "Sydney"
        return initialCost, dest
    else:
        print("Error")
        getDest2()

def getFare():
    option = input("Choose one of the following type of fare: " + "\n" + "(B) Business -- Extra $200" + "\n" + "(E) Economy -- Extra $50" + "\n" + "(F) Frugal -- Free")
    if option.upper() == "B":
        fare = 200
        return fare
    elif option.upper() == "E":
        fare = 50
        return fare
    elif option.upper() == "F":
        fare = 0
        return fare
    else:
        print("Error")
        getFare()

def getSeat():
    option = input("Choose one of the following type of seat: " + "\n" + "(W) Window -- $20" + "\n" + "(A) Aisle -- $15" + "\n" + "(M)Middle -- $10")
    if option.upper() == "W":
        seat = 20
        return seat
    elif option.upper() == "A":
        seat = 15
        return seat
    elif option.upper() == "M":
        seat = 10
        return seat
    else:
        print("Error")
        getSeat()

def getAge():
    age = int(input("Enter the age, if the age is bellow 17, you will get 50% discount >>> "))
    while age < 6 or age > 100:
        print("Invalid age")
        age= int(input("Enter the valid age >>>"))
    return age


def getTotalCost(price, fare, seat, age):
    if age <18:
        finalCost = (price + fare + seat)/2
    else:
        finalCost = price + fare + seat
    return finalCost


def loading():
    for i in range(101):
        time.sleep(0.02)
        sys.stdout.write("\r%d%%" % i)
        sys.stdout.flush()


def main2(name, done):
    getOption()
    nameForTicket = getTraveling2(name, done)
    price, destination = getWay()
    fare = getFare()
    seat = getSeat()
    age = getAge()
    totalcost = getTotalCost(price, fare, seat, age)
    print("\n" + "Thank you " + name + " for flying us!" + "\n" + "The ticket price is: $" + str(totalcost) + "\n" + "The destination is: " + str(destination) + "\n" + "Ticket is for: " + str(nameForTicket2).title())
    main2(name, done)

def getTraveling2(name, done):
    option = input("Who is the traveling person?" + "\n" + "(Y) You" + "\n" + "(S) Someone else")
    if option.upper() == "Y":
        if done == True:
            print("You have already ordered ticket for you!")
            getTraveling2(name, done)
        elif done == False:
            nameForTicket = name
            done = True
            return nameForTicket, done
    elif option.upper() == "S":
        nameForTicket = getName2()
        return nameForTicket
    else:
        print("Error")
        getTraveling2(name, done)

main()

短篇小说我正在编写这些长代码。

如果从def main() nameForTicket, done = getTraveling(name)我选择Y,则代码会在str(nameForTicket).title()中显示def main()的名称。

但是def main2():用于函数getTraveling2(name,done)如果我选择S,则str(nameForTicket2).title()中的def main2():将显示无。

如果从main()我选择S,main2()如果我选择S将不显示,如果我选择Y,它将显示(名称,真)

如何修复它,以便根据S?

的输入显示名称

1 个答案:

答案 0 :(得分:0)

main2中,更改

nameForTicket = getTraveling2(name, done)

nameForTicket2 = getTraveling2(name, done)

因为main2地址str(nameForTicket2).title()中的打印声明正在尝试将nameForTicket2方法的结果设置为{{1时打印getTraveling2的标题相反。因此,print语句为您提供nameForTicket,因为None从未真正定义过。