Python 插入函数和程序执行失败

Python 插入函数和程序执行失败,python,Python,我设计了一个小python程序 我希望通过for循环和insert函数实现以下效果 请在第1天输入存款:40 请在第2天输入押金:70 请在第3天输入押金:89 请在第4天输入押金:489 请在第5天输入押金:39 请在6:48输入押金 请在第7天输入押金:99 存款总额:874美元 通过for in-range循环设置介于1和7之间的天数 并通过插入功能插入(请输入当日存款():) 但是我的python程序无法顺利执行 我的代码: mey=list() sum1=0 尽管如此: moy=in

我设计了一个小python程序 我希望通过for循环和insert函数实现以下效果

请在第1天输入存款:40
请在第2天输入押金:70
请在第3天输入押金:89
请在第4天输入押金:489
请在第5天输入押金:39
请在6:48输入押金
请在第7天输入押金:99
存款总额:874美元
通过for in-range循环设置介于1和7之间的天数 并通过插入功能插入(请输入当日存款():) 但是我的python程序无法顺利执行 我的代码:

mey=list()
sum1=0
尽管如此:
moy=int(输入(“请输入第一天的存款:”)
对于范围(1,7)内的主运行中心:
最小插入值(主运行中心,4)
如果moc==7:
打破
sum1+=moy
打印(“存款总额:”,str(sum1),“美元”)
希望您能给予一些帮助和指导,
谢谢大家

试试这样的for循环

suml = 0
for _ in range(1,8):
    if _ in [4,5,7]:
        suml += int(input("Please enter the {}th day deposit: ".format(_)))
    else:
        suml += int(input("Please enter the deposit on day {}: ".format(_)))
print("Total deposit:",str(suml),"dollar")
使用生成器的简化版本:

print("Total deposit:",str(sum(int(input("Please enter the {}th day deposit: ".format(_))) if _ in [4,5,7] else int(input("Please enter the deposit on day {}: ".format(_))) for _ in range(1,8))),"dollar")
给你

sum = 0
for i in range(7):
    moy = int(input(f'Please enter the deposit for day{i + 1}: '))
    sum += moy

print(f'total deposit: ${sum}')

你可以通过这个简单的代码得到它

代码语法

根据
python3.8

输出

Enter how many days do you want to deposit: 3
please, Enter your deposit for day 1: 5.6
please, Enter your deposit for day 2: 5.4
please, Enter your deposit for day 3: 5
Your total deposit in (3) days is:  $16.0

[Program finished]

你有没有注意到差异,在某些情况下(对于4,5,7),它需要打印成第四、第五或第七个,请参阅我的答案检查我的答案。我只是简单地打印第1天,第2天等,这是正确的。没有必要为更高的数字加上“th”。我理解,谢谢。如果有帮助的话,请留下向上的一票。谢谢但它真的符合OP的预期输出吗?阅读问题的上半部分。顺便说一句,您也可以将此
alList
设置为
global-aList
,这样,如果您想从存储的数据中提取数据,数据将从总金额中减去。或者另一个想法。您还可以制作
dic
而不是
list
,这样,您可以随时编辑特定的日期,并将所有数据插入
.txt
文件,使用python读写实用程序操作数据。它似乎就像一个钱包,每次在存款和取款流程报告旁边计算和交换您想要的金额。请使用有意义的变量名称,而不是像
mey
moc
这样的胡说八道。如果moc==7:break,则无需使用
,循环到达范围结束时将自动停止。
moc==7
将永远不会为真,因为
range()
不包括第二个数字。
Enter how many days do you want to deposit: 3
please, Enter your deposit for day 1: 5.6
please, Enter your deposit for day 2: 5.4
please, Enter your deposit for day 3: 5
Your total deposit in (3) days is:  $16.0

[Program finished]