计算用户输入的数量

问题描述 投票:0回答:1

所以我的教授指派我编写一个Python程序来计算和监控灵活支出账户(FSA)。该计划应允许用户输入他们的 FSA 缴款并根据该金额跟踪他们的 FSA 支出,为他们提供有关支出状态的反馈。

这些是要求: 提示用户输入当年的 FSA 缴款总额。该值应该是整数并存储在变量中。在此变量的提示中,说明允许的最大捐款额为 3,200 美元。 实现一个 while 循环,提示用户输入 FSA 批准的每项支出的金额(也可以是整数)。 在 while 循环中,维护支出金额的运行总计以及已输入支出的计数。 当用户输入 0 作为支出金额时,while 循环应终止。 while 循环结束后,程序应打印支出总数、FSA 支出总额以及一条消息,指示用户的总支出是等于、高于还是低于其当年的 FSA 缴款总额。 我是编码初学者,所以请仁慈,但这就是我到目前为止所拥有的,但我现在陷入困境

# make a variable that represents the max number of the FSA contributions
# Prompt the user to  enter the expenditure amounts and save as 'expenditure
MAX_CONTRIB = 3200

contribution = int(input('Enter your FSA contribution for the year as a whole number (max allowed is 3200): '))
expenditure = int(input('Enter the FSA expenditure as a whole number (enter 0 to finish): '))
while expenditure > contribution:
    print(f'You have a count of {} FSA expenditures
    

python while-loop
1个回答
-1
投票

试试这个:

MAX_CONTRIB = 3200

contribution = int(input('Enter your FSA contribution for the year as a whole number (max allowed is 3200): '))

# Initialize variables
total_expenditure = 0
expenditure_count = 0

expenditure = int(input('Enter the FSA expenditure as a whole number (enter 0 to finish): '))

while expenditure != 0:
    total_expenditure += expenditure
    expenditure_count += 1
    expenditure = int(input('Enter the FSA expenditure as a whole number (enter 0 to finish): '))

print(f'Total count of expenditures: {expenditure_count}')
print(f'Total amount of FSA expenditures: {total_expenditure}')

# Check if total expenditures are at, above, or below the contribution
if total_expenditure == contribution:
    print("Your total expenditures match your FSA contribution for the year.")
elif total_expenditure < contribution:
    print("Your total expenditures are below your FSA contribution for the year.")
else:
    print("Your total expenditures are above your FSA contribution for the year.")

输出:

Enter your FSA contribution for the year as a whole number (max allowed is 3200): 2800
Enter the FSA expenditure as a whole number (enter 0 to finish): 2300
Enter the FSA expenditure as a whole number (enter 0 to finish): 200
Enter the FSA expenditure as a whole number (enter 0 to finish): 0
Total count of expenditures: 2
Total amount of FSA expenditures: 2500
Your total expenditures are below your FSA contribution for the year.
© www.soinside.com 2019 - 2024. All rights reserved.