Lee Shay
Lee Shay

Reputation: 11

How do I use a python table?

I was requested by my teacher to write a program in python which would require visitors of said attraction to insert the number of family members they are visiting with, and based off of that, have them know the admission fee. The admission fees given were $25 for anybody under 16, and 40 for anybody over 40. This is what I've got so far:

def main():
    admission = 0
    print("Welcome to Wally World - use this program to calculate your admission fees")
    number = int(input("How many members will you be taking with you? "))
    members = {}
    for n in range(0,number):
        age = int(input("Please enter your ages: "))
        if age <= 16:
            admission = 25
        if age > 16:
            admission = 40

main()

How would I go about grabbing said admission values and adding them all together? Thank you!

Upvotes: 1

Views: 94

Answers (1)

Shubham Namdeo
Shubham Namdeo

Reputation: 1925

  1. There is no need to use members = {}
  2. Admissions can be added with only single line conditional statement (ternary if-else).
  3. Check whether number of person is not negative or zero (I've used exit() in this case).
  4. Check whether age is not negative (I've put pass you may call your program again using main() or exit using exit()).

Here is the working code:

def main():
    admission = 0
    print("Welcome to Wally World - use this program to calculate your admission fees")
    number = int(input("How many members will you be taking with you? "))
    if number < 0: exit()
    for n in range(0,number):
        age = int(input("Please enter your ages: "))
        admission += (25 if age <= 16 and age > -1 else 40 if age> 16 else 0)
    return admission

main()

Upvotes: 1

Related Questions