简体   繁体   English

在一个函数中返回多个值

[英]Return multiple values in a function

I am doing a University project to create a plan ordering ticket program, so far these are what I have done:我正在做一个大学项目来创建一个计划订购票程序,到目前为止,这些是我所做的:

First, this is the function finding the seat type:首先,这是查找座位类型的函数:

def choosingFare():
    print("Please choose the type of fare. Fees are displayed below and are in addtion to the basic fare.")
    print("Please note choosing Frugal fare means you will not be offered a seat choice, it will be assigned to the ticketholder at travel time.")
    listofType = [""] * (3)

    listofType[0] = "Business: +$275"
    listofType[1] = "Economy: +$25"
    listofType[2] = "Frugal: $0"
    print("(0)Business +$275")
    print("(1)Economy +$25")
    print("(2)Frugal: $0")
    type = int(input())
    while type > 2:
        print("Invalid choice, please try again")
        type = int(input())
    print("Your choosing type of fare is: " + listofType[type])
    if type == 0:
        price1 = 275
    else:
        if type == 1:
            price1 = 25
        else:
            price1 = 0

    return price1, listofType[type]

And this is a function finding the destination:这是一个查找目的地的函数:

def destination():
    print("Please choose a destination and trip length")
    print("(money currency is in: Australian Dollars: AUD)")
    print("Is this a Return trip(R) or One Way trip(O)?")
    direction = input()
    while direction != "R" and direction != "O":
        print("Invalid, please choose again!")
        direction = input()
        print("Is this a Return trip(R) or One Way trip(O)?")
    if direction == "O":
        print("(0)Cairns oneway: $250")
        print("(2)Sydney One Way: $420")
        print("(4)Perth One Way: $510")
    else:
        print("(1)Cairns Return: $400")
        print("(3)Sydney Return: $575")
        print("(5)Perth Return: $700")
    typeofTrip = [""] * (6)

    typeofTrip[0] = "Cairns One Way: $250"
    typeofTrip[1] = "Cairns Return: $400"
    typeofTrip[2] = "Sydney One Way: $420"
    typeofTrip[3] = "Sydney Return: $575"
    typeofTrip[4] = "Perth One Way: $510"
    typeofTrip[5] = "Perth Return: $700"
    trip = int(input())
    while trip > 5:
        print("Invalid, please choose again")
        trip = int(input())
    if trip == 0:
        price = 250
    else:
        if trip == 1:
            price = 400
        else:
            if trip == 2:
                price = 420
            else:
                if trip == 3:
                    price = 574
                else:
                    if trip == 4:
                        price = 510
                    else:
                        price = 700
    print("Your choice of destination and trip length is: " + typeofTrip[trip])

    return price, typeofTrip[trip]

And this is the function calculating the total price:这是计算总价的函数:

def sumprice():
    price = destination()
    price1 = choosingFare()
    price2 = choosingseat()
    sumprice = price1 + price2 + price
    print("How old is the person travelling?(Travellers under 16 years old will receive a 50% discount for the child fare.)")
    age = float(input())
    if age < 16 and age > 0:
        sumprice = sumprice / 2
    else:
        sumprice = sumprice

    return sumprice

The error I have:我有的错误:

line 163, in <module> main()
line 145, in main sumprice = sumprice()
line 124, in sumprice
sumprice = price1 + price2 + price
TypeError: can only concatenate tuple (not "int") to tuple

Can someone help me?有人能帮我吗? I am really stuck.我真的被困住了。

I can't return all the我不能全部归还

  • First let me explain what happends when you write.首先让我解释一下你写作时发生了什么。 return price, typeofTrip[trip] . return price, typeofTrip[trip]
  • The above line will return a tuple of two values.上面的行将返回一个包含两个值的元组。
  • Now for sumprice I think what you want is sum of all prices.现在对于 sumprice 我认为你想要的是所有价格的总和。 So you just want to sum first element of returned values.所以你只想对返回值的第一个元素求和。
  • This should work for your case.这应该适用于您的情况。
sumprice = price1[0] + price2[0] + price3[0]

These functions return 2 values each: destination(), choosingFare(), choosingseat().这些函数各自返回 2 个值:destination()、choingFare()、chooseseat()。

Returning multiple values at once returns a tuple of those values: For example:一次返回多个值会返回这些值的元组:例如:

 return price, typeofTrip[trip] # returns (price, typeofTrip[trip])

So while calculating the sum of all prices, you need to access price, price1, price2 from the tuples:因此,在计算所有价格的总和时,您需要从元组中访问 price、price1、price2:

sumprice = price1[0] + price2[0] + price3[0]

Alternatively: You can edit the code to return list/ dictionary or some other data structure as per your convenience.或者:您可以根据自己的方便编辑代码以返回列表/字典或其他一些数据结构。

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

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