编写一个程序,根据控制台输入的事务日志计算银行帐户的净金额。事务日志格式如下:
D 100
W 200
D表示存款,W表示取款。
假设向程序提供了以下输入:
D 300型
D 300型
宽200
D 100个
那么,输出应该是:
500
enter code here
tot = 0
n = int(input())
i = 0
while(i < n):
x = input()
values = x.split(" ")
operation = values[0]
amount = int(values[1])
if operation == "D":
tot += amount
elif operation == "W":
tot -= amount
else:
pass
i += 1
print("total=", tot)
我的第一个建议是尝试调试代码。你知道吗
什么是n?这是你所期望的吗?你知道吗
什么是x?这是你所期望的吗?你知道吗
values = x.split(" " )
在做你认为应该做的事情吗?你知道吗我猜是输入格式不正确,但如果没有额外的信息,很难说到底出了什么问题。你知道吗
使用系统标准而不是这里的input()。。。 这样可以更容易地循环输入行,而不是读取input()两次。它更简洁,更容易理解和调试。你知道吗
相关问题 更多 >
编程相关推荐