简体   繁体   中英

Unsupported Opperand Type Error

(EDITED) I am trying to make a piggybank by saving the previous money amounts to a file so you can access how much money you have previously had. But, it is giving me an error(see title). Please don't mark this as a duplicate because I already checked the others and they don't apple to my problem. Here is my code:

 def piggybank():
     newamount = 0.0
     file = open('piggybank.txt','r+')
     addedmoney = input('How much money are you adding?')
     file.write(addedmoney + '\n')
     for line in file:
         newamount += line
     print("You now have:\n", newamount)

Basically I am saying that the new amount is 0. Then I open my file in read and write mode and ask how much the user wants to add. Then I add it to a new line on my file and add up everything in the file. Lastly, I print the sum. However, this does not work because I keep getting the error. Please Help!

(I am sort of a noob at Python and Stack Overflow because I am 13 and just started learning.)

Here is my new code:

 def piggybank():
    file = open('piggybank.txt','r+')
    money = input('How much money are you adding?')
    file.write(money + '\n')
    for line in file:
        money += line
    print("You now have:\n", money)
    file.close()

If you look at my original code, I added a newline to money and I did that here as well. However, it adds the money strings as if they were strings so it gives '5.005.00' if you enter 5.00 twice. Does anyone know how to add a new line if you want to print numbers and not strings?

It's because your line is string and newamount is number. That's why you get the error. You have to convert the string to number first before proceeding the math calculation.

def piggybank():
  newamount = 0.0
  file = open('piggybank.txt', 'r+')
  addedmoney = input('How much money are you adding?')
  file.write(str(addedmoney) + '\n')
  file.seek(0)
  for line in file:
    newamount += float(line)
  print("You now have:\n", newamount)
 def piggybank():
     newamount = 0.0 # newamount is a floating point number
     file = open('piggybank.txt','r+')
     addedmoney = input('How much money are you adding?')
     file.write(addedmoney + '\n')
     for line in file: # line is a string
         newamount += line # you are trying to add a floating point number to a string, so the error (I assume) happens here.
     print("You now have:\n", newamount)

In conclusion, the operands do not match. First you must convert the string to a floating point number like so:

newamount += float(line.strip()) # strip removes trailing whitespace

On another note, why write addedmoney to the file if you only need to store the total? You can try calculating newamount first and then proceed write that result.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM