這是我的代碼。我不明白爲什麼我會得到這個錯誤以及如何糾正它。 出現錯誤:totals = entrant + float(tot)
這裏是我的全碼:值錯誤:無法將str轉換爲浮點數PYTHON
def total():
File = open("argent.txt","r")
File = File.read()
tot = File
print("You have",tot,"£ in your account")
def add():
entrant = float(input("How many do you want to add to your account? "))
with open("argent.txt", 'r') as f:
tot = f.read().rstrip('\n')
print("You have ",tot,"£ in your account")
totals = entrant + float(tot)
print(totals)
with open("argent.txt", 'w') as f:
output = str(totals)
f.write(output)
add()
在此先感謝。
'tot'不是一個有效的float – Li357
提供文件內容的樣品或以上 – praba230890
的'print'聲明的文件只是一個第一行有20個文本文件。 –