0

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

martineau
  • 99,260
  • 22
  • 139
  • 249
  • Does this answer your question? [How do I return multiple values from a function?](https://stackoverflow.com/questions/354883/how-do-i-return-multiple-values-from-a-function) – Eric Le Fort Jan 18 '20 at 06:55
  • `price, typeofTrip = destination()` , `prince1, typeofFare = choosingFare()` – furas Jan 18 '20 at 07:06
  • BTW: you should start with `print( price )` to see what you get from function – furas Jan 18 '20 at 07:08

2 Answers2

0
  • First let me explain what happends when you write. 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. So you just want to sum first element of returned values.
  • This should work for your case.
sumprice = price1[0] + price2[0] + price3[0]
Poojan
  • 2,985
  • 2
  • 13
  • 31
0

These functions return 2 values each: destination(), choosingFare(), choosingseat().

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:

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.

Suyash
  • 124
  • 3
  • 12