我下面的代码没有达到预期的点。你知道吗
print("Welcome to the Stockroom\n")
print("The following products need new stock:\n")
f = open("lowstock","r")
print(f.read())
choice = input("\nTo update the stock levels of the above products, type 1. To cancel, enter anything else.")
if choice == '1':
with open('stockcontrol.csv',newline='') as f:
for line in f:
data = line.split(",")
productcode = int(data[0])
target = int(data[2])
stocklevel = int(data[1])
if stocklevel <= 5:
target = str(target)
import sys
import csv
data=[]
# code = code
newval= target
newtlevel = "0"
f=open("stockcontrol.csv")
reader=csv.DictReader(f,fieldnames=['code','level', 'target', 'distancefromtarget'])
for line in reader:
line['level']= newval
line['distancefromtarget']= newtlevel
data.append('%s,%s,%s,%s'%(line['code'],line['level'],line['target'],line['distancefromtarget']))
f.close()
f=open("stockcontrol.csv","w")
f.write("\n".join(data))
f.close()
print("The stock levels were updated successfully")
else:
print("Goodbye")
如果在选项输入中输入1,程序就终止了-知道为什么不替换csv中的字符串吗?是什么阻止了它发展到这个阶段?你知道吗
在python3中,
input
函数总是返回一个字符串。因此,如果用户输入1
,choice
将是"1"
,而不是您正在测试的整数1
。你知道吗你似乎编辑了这个问题来改变错误的方向,原因我不清楚。您要么想要
int(input())
,要么需要针对"1"
进行测试。(可能还有其他问题,但这是目前代码的主要问题。)相关问题 更多 >
编程相关推荐