如何在python中使用空行结束程序? fee=[] 年龄=整数(输入(“输入年龄:”) 而年龄!='': 年龄=整数(输入(“输入年龄:”) 如果年龄=6且年龄费用=0 年龄=整数(输入(“输入年龄:”) 而年龄!='': 尝试: age=int(输入(“输入年龄:”)//以避免字符串异常 除: 打破 如果age=6且age

如何在python中使用空行结束程序? fee=[] 年龄=整数(输入(“输入年龄:”) 而年龄!='': 年龄=整数(输入(“输入年龄:”) 如果年龄=6且年龄费用=0 年龄=整数(输入(“输入年龄:”) 而年龄!='': 尝试: age=int(输入(“输入年龄:”)//以避免字符串异常 除: 打破 如果age=6且age,python,blank-line,Python,Blank Line,无法将字符串与整数进行比较,这是您的主要问题。如果您将它作为字符串从用户处检索,并检查它是否确实是整数,则会起作用。这段代码应该可以做到: fee = 0 age = int(input("Enter age: ")) while age != '': try: age = int(input("Enter age: ")) // to avoid exception of string except: break if age &l

无法将字符串与整数进行比较,这是您的主要问题。如果您将它作为字符串从用户处检索,并检查它是否确实是整数,则会起作用。这段代码应该可以做到:

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)
def代表:
尝试:
int(s)
返回真值
除值错误外:
返回错误
费用=[]
r='start'
而r!='':
r=输入(“输入年龄:”)
如果代表(r):
年龄=int(r)

如果年龄=6且年龄在第一次输入操作之后,
age
是一个整数。然后将该整数与空字符串进行比较。你希望这两个什么时候是一样的?顺便说一句,作为这里的新用户,请阅读。特别是,你甚至连一个问题都没有问。还有,如果连你自己都不知道自己是否“做得正确”,其他人怎么会知道呢?在这种情况下,你对“正确”的定义是什么?
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)
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)