-2
所以我堅持這個代碼,它的一點是,你輸入汽車名稱和汽車的數量,你想租,但我可以弄清楚如何更改字符串之後你.txt文件租車python:如何更改.txt文件中的字符串與輸入
def rental():
with open('cars.txt', 'r') as file: # cars.txt example:
# bmw:320:red:new:6
name = input('Car name: ')
for line in file:
s = line.split(':')`enter code here`
if name == s[0] :
amount = eval(input('Amount of cars'))
if amount > int(s[4]):
print('Amount is too big')
else:
t = str(int(s[4])-int(kolicina))
line.replace(s[4], t)
else:
print('Car does not exist')
你應該看看[問] – pvg
[如何修改文本文件?](https://stackoverflow.com/questions/125703/how-to-modify-a-text-file) – alfasin
也許OT,但是這段代碼需要重構:在處理文件之前,您應該詢問並驗證用戶輸入 – Don