我正在尝试建立银行,但我无法更改文件的整数数据。 我的文件包含:
astle,hello,11
monish,mr7,0
现在在我的' choice1'(稍后在代码中显示)当我尝试存款时假设为100,它不会更改值而是给出相同的值。就像我输入我的情况一样用户名为' astle'并尝试存入100,价值变为100而不是111.我想要111存入它应该是旧值+ newvalue。
我尝试了以下代码,但无济于事。
这是我的代码:
import csv
run=True
while run==True:
print "1) Deposit money"
choice=input("Enter your choice over here: ")
#This part takes the input from the user and then stores it in 'store.txt'
if choice==1:
b=raw_input("Enter the username: " )
with open('store.txt','r') as f:
reader = list(csv.reader(f))
for row in reader:
if b==row[0]: #Check the username with the file
c=input("Enter your new ammount")
print row[2],"Old value"
a=int(row[2])
print a
row[2] = c
print row[2],"New value"
flag = 1
break
else:
flag=0
if flag==0:
print "Username does not exist."
#Writes the new value in the file
with open('store.txt','w') as f:
wr = csv.writer(f)
for row in reader:
wr.writerow(row)
我尝试用行[2] + = c替换row [2] = c,但它给出了以下错误:
row[2]+=d
TypeError: cannot concatenate 'str' and 'int' objects
答案 0 :(得分:0)
您需要将值从str
转换为int
。试试这个。
row[2] = str(int(row[2]) + c)