简体   繁体   English

如何使用 python 表?

[英]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.我的老师要求我用 python 编写一个程序,该程序将要求所述景点的游客插入他们正在拜访的家庭成员的数量,并据此让他们知道入场费。 The admission fees given were $25 for anybody under 16, and 40 for anybody over 40. This is what I've got so far: 16 岁以下的人收取 25 美元的入场费,40 岁以上的人收取 40 美元的入场费。这是我目前得到的:

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!谢谢!

  1. There is no need to use members = {}没有必要使用members = {}
  2. Admissions can be added with only single line conditional statement ( ternary if-else ).只能使用单行条件语句(三元 if-else )添加录取。
  3. Check whether number of person is not negative or zero (I've used exit() in this case).检查人数是否为负数或零(在这种情况下我使用了exit() )。
  4. Check whether age is not negative (I've put pass you may call your program again using main() or exit using exit() ).检查年龄是否不是负数(我已经把pass你可以使用main()再次调用你的程序或使用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()

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

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