The Artima Developer Community
Sponsored Link

Python Answers Forum
writing a number variable to a file

6 replies on 1 page. Most recent reply: Mon, Mar 18 5:13 AM by TopEssay Services

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 6 replies on 1 page
not telling

Posts: 2
Nickname: ulrich200
Registered: Oct, 2008

writing a number variable to a file Posted: Oct 18, 2008 6:04 PM
Reply to this message Reply
Advertisement
Hi I'm new to python and I'm trying to make a program to keep track of my money, but when it tries to write the number i get this message:

TypeError: unsupported operand type(s) for +: 'float' and 'str'

here is the script, can someone please help me?

moneyRead = open("my_money.txt", "r")
moneyWrite = open("my_money.txt", "w")
choice = 0
choice2 = 0
loop = 1
value = 0
value1 = 0
value2 = 0
value3 = 0

print " "
print "Welcome!"
print " "
print "To view the amount of money you have, type: 1"
print "To set the amount of money you have, type: 2"
print "To add or subtract to the amount of money you have, type: 3"
print "to quit, type: 4"
print " "
while loop == 1:
choice = input("What do you want to do?: ")
print " "

if choice == 1:
print moneyRead.readline()

if choice == 2:
value = input("How much money do you have?: ")
moneyWrite.write(value)

if choice == 3:
print "The current saved amount is " + moneyRead.readline()
print " "
print "To add to this amount, type: 1"
print "To subtract from this amount, type: 2"
choice2 = input("What do you want to do?: ")

if choice2 == 1:
value1 = moneyRead.readline()
value2 = input("how much do you want to add?: ")
value3 = value1 + value2
moneyWrite.write(value3)
if choice2 == 2:
value1 = moneyRead.readline()
value2 = input("how much do you want to subtract?: ")
value3 = value1 - value2
moneyWrite.write(value3 )

if choice == 4:
loop = 0


not telling

Posts: 2
Nickname: ulrich200
Registered: Oct, 2008

Re: writing a number variable to a file Posted: Oct 18, 2008 6:12 PM
Reply to this message Reply
I messed up on the error message.

here's the real error message:

TypeError: argument 1 must be string or read-only character buffer, not float

ndoe ganteng

Posts: 3
Nickname: ndoe
Registered: Oct, 2008

Re: writing a number variable to a file Posted: Nov 3, 2008 9:22 AM
Reply to this message Reply
how to make a paragraph from file to file

like this e.g data.txt

i
want
eat
some
pizza
with
cappucino
in
kuta
bali



to this data2.txt

i want eat some pizza with cappucino in kuta bali

but without exchange the code i free change data.txt

e.g

i
want
eat
some
pizza
with
cappucino
in
kuta
bali
late
night

i want eat some pizza with cappucino in kuta bali late night

captain eddy

Posts: 4
Nickname: hemsbond
Registered: Oct, 2010

Re: writing a number variable to a file Posted: Nov 8, 2010 1:04 AM
Reply to this message Reply

moneyRead = open("my_money.txt", "r")
moneyWrite = open("my_money.txt", "w")
choice = 0
choice2 = 0
loop = 1
value = 0
value1 = 0
value2 = 0
value3 = 0

print " "
print "Welcome!"
print " "
print "To view the amount of money you have, type: 1"
print "To set the amount of money you have, type: 2"
print "To add or subtract to the amount of money you have, type: 3"
print "to quit, type: 4"
print " "
while loop == 1:
choice = input("What do you want to do?: ")
print " "

if choice == 1:
print moneyRead.readline()

if choice == 2:
value = raw_input("How much money do you have?: ")
moneyWrite.write(value)

if choice == 3:
print "The current saved amount is " + moneyRead.readline()
print " "
print "To add to this amount, type: 1"
print "To subtract from this amount, type: 2"
choice2 = input("What do you want to do?: ")

if choice2 == 1:
value1 = moneyRead.readline()
value2 = raw_input("how much do you want to add?: ")
value3 = value1 + value2
moneyWrite.write(value3)
if choice2 == 2:
value1 = moneyRead.readline()
value2 = raw_input("how much do you want to subtract?: ")
value3 = value1 + value2
moneyWrite.write(value3 )

if choice == 4:
loop = 0

Vijay Khatri

Posts: 2
Nickname: vijay1994
Registered: Dec, 2018

Re: writing a number variable to a file Posted: Dec 12, 2018 2:07 AM
Reply to this message Reply
Any of these should work

outf.write("%s" % num)

outf.write(str(num))

print >> outf, num

To learn more: https://hackr.io/blog/10-best-python-books-for-beginners-and-advanced-programmers

Kyle Dowson

Posts: 1
Nickname: da5tin
Registered: Jan, 2019

Re: writing a number variable to a file Posted: Jan 29, 2019 8:01 AM
Reply to this message Reply
Perhaps in this example more clearly explains how to do this:
https://codereview.stackexchange.com/questions/173486/simple-budget-program

Also, you can find useful info at assignment.essayshark.com/accounting-help.html (about finances and accounting) and https://www.oreilly.com/learning/algorithmic-trading-in-less-than-100-lines-of-python-code in general.

TopEssay Services

Posts: 1
Nickname: topessay
Registered: Mar, 2019

Re: writing a number variable to a file Posted: Mar 18, 2019 5:13 AM
Reply to this message Reply
I am sure someone will offer some possible solutions soon. I wanted to recommend a service that saved me more than once - https://www.topessayservices.com/studybay_review/

Flat View: This topic has 6 replies on 1 page
Topic: Affiliate marketing: Custom coupon codes Previous Topic   Next Topic Topic: writing string variable to file


Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2018 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use