Joe han
Joe han

Reputation: 171

How to use return answer from other function to the next one?

I have this program which I want to calculate the price for the number of tables with the menu packages:

noPeople = int(input('enter no. of people:'))
def calculateTableTotal(noPeople):
    if noPeople <= 15:
        return('Tables:',1)

    elif (noPeople >= 16) or (noPeople <= 35):
        return('Tables:',3)

    elif (noPeople >= 36) or (noPeople <= 55):
        return('Tables:',5)

    elif (noPeople >= 56) or (noPeople <= 75):
        return('Tables:',7)
calculateTableTotal(noPeople)

print('''
-------------------------------------------------------------
Menu Option
-------------------------------------------------------------

[1] 768.88 Package          [3] 1118.88 Package
[2] 898.88 Package          [4] 1488.88 Package
''')

totalTable = calculateTableTotal(noPeople)
choice = input('enter menu choice:')
def calculateMenuPrice(totalTable,choice):
    if choice == '1':
        print('Total:',totalTable*(int(768.88)))

    if choice == '2':
        print('Total:',totalTable*(int(898.88))

    if choice == '3':
        print('Total:',totalTable*(int(1118.88))

    if choice == '4':
        print('Total:',totalTable*(int(1488.88)))

calculateMenuPrice(totalTable,choice)

It seems that the answer keeps on repeating the (return('tables:')):

enter no. of people:34

-------------------------------------------------------------
Menu Option
-------------------------------------------------------------

[1] 768.88 Package          [3] 1118.88 Package
[2] 898.88 Package          [4] 1488.88 Package

enter menu choice:1

    ('Tables:', 3, 'Tables:', 3, 'Tables:', 3, 'Tables:', 3, 'Tables:', 3, 'Tables:', 3, 'Tables:', 3, 'Tables:', 3, 'Tables:', 3, 'Tables:', 3, 'Tables:', 3, 'Tables:', 3, 'Tables:', 3, 'Tables:', 3, 'Tables:', 3, 'Tables:', 3, 'Tables:', 3, 'Tables:', 3, 'Tables:', 3, 'Tables:', 3, 'Tables:', 3, 'Tables:', 3, 'Tables:', 3, 'Tables:', 3, 'Tables:', 3, 'Tables:', 3, 'Tables:', 3, 'Tables:', 3, 'Tables:', 3, 'Tables:', 3, 'Tables:', 3, 'Tables:', 3, 'Tables:', 3, 'Tables:', 3, 'Tables:', 3, 'Tables:', 3, 'Tables:', 3, 'Tables:', 3, 'Tables:', 3, 'Tables:', 3, 'Tables:', 3, 'Tables:', 3, 'Tables:', 3, 'Tables:', 3, 'Tables:', 3, 'Tables:', 3, 'Tables:', 3, 'Tables:', 3, 'Tables:', 3, 'Tables:', 3, 'Tables:', 3, 'Tables:', 3, 'Tables:', 3, 'Tables:', 3, 'Tables:', 3, 'Tables:', 3, 'Tables:', 3, 'Tables:', 3, 'Tables:', 3, 'Tables:', 3,.....

What should I fix to my program so that it will calculate the price for the menu package with the number of tables:

enter no. of people:34

    -------------------------------------------------------------
    Menu Option
    -------------------------------------------------------------

    [1] 768.88 Package          [3] 1118.88 Package
    [2] 898.88 Package          [4] 1488.88 Package

    enter menu choice:1
    Total:2306.64

Help please. I'm new to python. Thanks

Upvotes: 0

Views: 49

Answers (3)

JerryTheo
JerryTheo

Reputation: 91

First off, your code is has a bunch of issues. I'm certain you would rather have calculateTableTotal(noPeople) defined this way,

def calculateTableTotal(noPeople):
    if noPeople <= 15:
        return('Tables:', 1)

    elif noPeople <= 35:
        return('Tables:', 3)

    elif noPeople <= 55:
        return('Tables:', 5)

    elif noPeople <= 75:
        return('Tables:', 7)

Or, you could use and instead of or. If you use or, the first elif will always be true, regardless of the value of noPeople because every number is either greater than 16 or less than 35.

As for your issue, the reason you're having multiple values is because in python, multiplying a list/tuple with an integer creates a new list/tuple with the values repeated as many times as the integer specifies. For e.g.,

print([1, 2] * 3)

will give you the output,

[1, 2, 1, 2, 1, 2]

You could use this if you really wish to have 'Tables' as a part of the returned value,

table, totalTable = calculateTableTotal(noPeople)
choice = input('enter menu choice:')
calculateMenuPrice(totalTable,choice)

Or you, preferably, could do what Carcigenicate says and change the return value.

Upvotes: 1

Carcigenicate
Carcigenicate

Reputation: 45741

Your problem is the return of your function, for example

('Tables:',1)

is a tuple (a container), not a number. You're doing multiplication on the returned tuple, not a number. Just get rid of the "Tables:" part.

Change all your returns from

return('Tables:',1)

To

return 1

I'm not sure why you had wrapped the return in a tuple and gave it a "Tables" element, but that's surely not what you wanted to get that end goal.


And as noted in the comments, your code also contains syntax errors; you're missing a ) on two of your lines. (int(898.88)) should be totalTable*(int(898.88))), and the line below it needs a closing parentheses as well.

Upvotes: 3

M14
M14

Reputation: 490

I've corrected your source code a little bit. Only some comments.

I don't understand everything, what you do! Some examples: If you have 16 people, then they need 3 tables in your restaurant, but if there are 35 people (16 * 2 = 32 people!!!), then they still need 3 tables? These tables must be impressive.

Why do you give your customers some money for free!? Why do you calculate with integers ("int(768.88)"), when you could calculate better with floating numbers???

I dislike your menu code, I would think to store the data in a dictionary and to make the menu from there dynamically and use the same data to calculate the prices. At the moment you have the price data two times statically in your source code - don't repeat yourself.

def calculateTableTotal(noPeople):
    if noPeople <= 15:
        return 1 
    elif 16 <= noPeople <= 35:
        return 3
    elif 36 <= noPeople <= 55:
        return 5
    elif 56 <= noPeople <= 75:
        return 7
    else:
        return None

def calculateMenuPrice(totalTable, choice):
    if choice == '1':
        print('Total:', totalTable * (int(768.88)))  # ???

    if choice == '2':
        print('Total:', totalTable * (int(898.88)))  # ???

    if choice == '3':
        print('Total:', totalTable * (int(1118.88))) # ???

    if choice == '4':
        print('Total:', totalTable * (int(1488.88))) # ???

def print_menu():
    print('''
    ------------------------------------------------
    Menu Option
    ------------------------------------------------

    [1] 768.88 Package          [3] 1118.88 Package
    [2] 898.88 Package          [4] 1488.88 Package
    ''')     

noPeople = int(input('enter no. of people: '))

totalTable = calculateTableTotal(noPeople)

if totalTable == 1:
    print("You need {} table.".format(totalTable))
elif totalTable is None:
    print("We haven't enough tables. Sorry!")
    exit()
else:
    print("You need {} table.".format(totalTable))

print_menu()

choice = input('enter menu choice: ')

calculateMenuPrice(totalTable, choice)

Upvotes: 0

Related Questions