繁体   English   中英

Python-打印名称显示无

[英]Python - print name shows None

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()

简短的故事,我正在写这些长代码。

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

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

如果从main()选择S, main2()发生同样的情况,如果选择S的main2()将不显示任何内容,如果选择Y,则将显示(Name,True)

如何解决这个问题,以便根据S的输入显示名称?

main2 ,更改

nameForTicket = getTraveling2(name, done)

nameForTicket2 = getTraveling2(name, done)

因为在你的打印语句main2地址str(nameForTicket2).title()它正在试图打印的标题nameForTicket2当您设置的结果getTraveling2方法nameForTicket代替。 因此,由于从未真正定义过nameForTicket2 ,所以print语句为您提供None

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM