Python打印名显示非

2024-06-01 08:27:14 发布

您现在位置:Python中文网/ 问答频道 /正文

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()的名称就可以了。你知道吗

但是在函数getTraveling2(name,done)的def main2():中,如果选择S,def main2():中的str(nameForTicket2).title()将显示None。你知道吗

如果我从main()中选择S,也会发生同样的情况,如果我选择S,main2()将显示no,如果我选择Y,它将显示(Name,True)

如何修复它,以便根据S的输入显示名称?你知道吗


Tags: nameagereturnifdefupperdestinationelse
1条回答
网友
1楼 · 发布于 2024-06-01 08:27:14

main2中,更改

nameForTicket = getTraveling2(name, done)

nameForTicket2 = getTraveling2(name, done)

因为main2中的print语句地址是str(nameForTicket2).title(),当您将getTraveling2方法的结果改为nameForTicket时,它试图打印nameForTicket2的标题。因此,print语句提供了None,因为nameForTicket2从未真正定义过。你知道吗

相关问题 更多 >