繁体   English   中英

如何在python中使用空行结束程序?

[英]How to end a program using blank line in python?

fee = []

age = int(input("Enter age: "))

while age != '':

    age = int(input("Enter age: "))

    if age <= 5:
        fee.append(0)
    elif age >= 6 and age <= 64:
        fee.append(50.00)
    else:
        fee.append(25.00)

total = sum(fee)
print("Total payment: ", total)

我想制作一个程序来总结给定的入场费。 我什至不知道我是否做对了

您无法将字符串与整数进行比较,这是您的主要问题。 如果您从用户那里以字符串形式检索它并检查它是否确实是一个整数将起作用。 该代码应该可以解决问题:

def RepresentsInt(s):
    try: 
        int(s)
        return True
    except ValueError:
        return False

fee = []

r='start'

while r != '':

    r = input("Enter age: ")
    if RepresentsInt(r):
        age = int(r)
        if age <= 5:
            fee.append(0)
        elif age >= 6 and age <= 64:
            fee.append(50.00)
        else:
            fee.append(25.00)

total = sum(fee)
print("Total payment: ", total)
fee = 0

age = int(input("Enter age: "))

while age != '':
    try:
        age = int(input("Enter age: "))  // to avoid exception of string
    except:
        break
    if age <= 5:
        fee += 0  // no need to append if outcome is a number
    elif age >= 6 and age <= 64:
        fee += 50
    else:
        fee += 25

total = fee
print("Total payment: ", total)

暂无
暂无

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

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