Python Forum
little problem with read out and write into a file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
little problem with read out and write into a file
#1
Hi, I am trying to program an ATM but I got a problem because I want to save and read out the available money but I need to calculate the number that is in the .txt file I experimented a bit but I can't figure it out. Sorry that my variables and everything else is in German but you should understand what I am trying to do, if not I will explain it. So what I am trying to do is read out the file "kontostand.txt" in which the amount of money is in and calculate -50 for -50€ but I can't calculate the output or a string but I am not able to convert it to a string it gives this error: rechnung_kontostand = int(zwischenschritt)
ValueError: invalid literal for int() with base 10: "<_io.TextIOWrapper name='kontostand.txt' mode='r' encoding='UTF-8'>". Another problem is that what is saved to my .txt is "-50".

thanks


main = 1
behebung = 0
rechnung_kontostand = 0
ergebniss_kontostand = 0
ergebniss_kontostand2 = ""
while main == 1:
	main = 0
	print("Möchten Sie \n 1) Beheben \n 2) Einzahlen \n 3) Kontostand \n 4) Bewegungen \n 5) beenden")
	auswahl = input()

	
	if auswahl == "1":
		behebung = 1
		while behebung == 1:
			behebung = 0
			behebung_auswahl = input("Wieviel möchten Sie beheben?\n 1)50€ \t \t \t \t 2)100€ \n 3)200€ \t \t \t \t 4)500€ \n 5)800€ \t \t \t \t 6)1000€ \n")
			auslesen_kontostand = open("kontostand.txt", "r")
			zwischenschritt = str(auslesen_kontostand)
			auslesen_kontostand.close()
			rechnung_kontostand = int(zwischenschritt)
			if auslesen_kontostand == "":
				print("Sie haben kein Konto!")
				behebung = 0
				main = 1

			if auslesen_kontostand != "":
		
				if behebung_auswahl == "1" and rechnung_kontostand >= -2250:
					ergebniss_kontostand = rechnung_kontostand - 50
					ergebniss_kontostand2 = str(ergebniss_kontostand)
					print("Entnehmen Sie Ihre 50€")
					auslesen_kontostand = open("kontostand.txt", "w")
					auslesen_kontostand.write(ergebniss_kontostand2)
					auslesen_kontostand.close()
					auswahl = ""
					main = 1

		
	if auswahl == "5":
		main = 0
		print("Bis zum nächsten mal!")

	if auswahl != "1" and auswahl != "2" and auswahl != "3" and auswahl != "4" and auswahl != "5":
		print("Bitte geben Sie eine gültige Operation ein!")
		main = 1
Reply
#2
for read change lines 17 - 24:
            auslesen_kontostand = open("kontostand.txt", "r")
            zwischenschritt = str(auslesen_kontostand)
            auslesen_kontostand.close()
            rechnung_kontostand = int(zwischenschritt)
            if auslesen_kontostand == "":
                print("Sie haben kein Konto!")
                behebung = 0
                main = 1
to:
            with open("kontostand.txt") as fin:
                for line in fin.readlines()
                rechnung_kontostand = int(zwischenschritt)
                if auslesen_kontostand == "":
                    print("Sie haben kein Konto!")
                    behebung = 0
                    main = 1
for write, change lines 32-34:
                    auslesen_kontostand = open("kontostand.txt", "w")
                    auslesen_kontostand.write(ergebniss_kontostand2)
                    auslesen_kontostand.close()
to:
                    with open("kontostand.txt", "w") as f:
                        f.write(ergebniss_kontostand2)
It's easier to understand, and doesn't require a close (closes automatically)

I have no way to test, so there may be errors, but you should be able to figure out what they are, if not ask.
Reply
#3
It gives me this error :
File "bankomat.py", line 18
for line in fin.readlines()
^
TabError: inconsistent use of tabs and spaces in indentation

My code looks like that now:
main = 1
behebung = 0
rechnung_kontostand = 0
ergebniss_kontostand = 0
ergebniss_kontostand2 = ""
while main == 1:
	main = 0
	print("Möchten Sie \n 1) Beheben \n 2) Einzahlen \n 3) Kontostand \n 4) Bewegungen \n 5) beenden")
	auswahl = input()

	
	if auswahl == "1":
		behebung = 1
		while behebung == 1:
			behebung = 0
			behebung_auswahl = input("Wieviel möchten Sie beheben?\n 1)50€ \t \t \t \t 2)100€ \n 3)200€ \t \t \t \t 4)500€ \n 5)800€ \t \t \t \t 6)1000€ \n")
			with open("kontostand.txt") as fin:
                for line in fin.readlines()
                rechnung_kontostand = int(zwischenschritt)
                if auslesen_kontostand == "":
                    print("Sie haben kein Konto!")
                    behebung = 0
                    main = 1

			if auslesen_kontostand != "":
		
				if behebung_auswahl == "1" and rechnung_kontostand >= -2250:
					ergebniss_kontostand = rechnung_kontostand - 50
					ergebniss_kontostand2 = str(ergebniss_kontostand)
					print("Entnehmen Sie Ihre 50€")
					auslesen_kontostand = open("kontostand.txt", "w")
					with open("kontostand.txt", "w") as f:
                        f.write(ergebniss_kontostand2)
						main = 1

		
	if auswahl == "5":
		main = 0
		print("Bis zum nächsten mal!")

	if auswahl != "1" and auswahl != "2" and auswahl != "3" and auswahl != "4" and auswahl != "5":
		print("Bitte geben Sie eine gültige Operation ein!")
		main = 1
Reply
#4
Is that all? Do you get any output at all other than the error.
Please post error message verbatim as it contains valuable information.
As I stated earlier,
Quote:I have no way to test, so there may be errors, but you should be able to figure out what they are

You should have been able to find the missing colon at the end of line 18!
Reply
#5
Missing ':' at the end of the for loop statement
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#6
It isn't running and just gives me the error message I posted alredy :-( and the ":" didn't solve the problem
Reply
#7
The error message makes it clear. You are using both tabs and spaces for the indentation. Set the IDE to put spaces every time you hit TAB.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#8
main = 1
behebung = 0
rechnung_kontostand = 0
ergebniss_kontostand = 0
ergebniss_kontostand2 = ""
while main == 1:
	main = 0
	print("Möchten Sie \n 1) Beheben \n 2) Einzahlen \n 3) Kontostand \n 4) Bewegungen \n 5) beenden")
	auswahl = input()

	
	if auswahl == "1":
		behebung = 1
		while behebung == 1:
			behebung = 0
			behebung_auswahl = input("Wieviel möchten Sie beheben?\n 1)50€ \t \t \t \t 2)100€ \n 3)200€ \t \t \t \t 4)500€ \n 5)800€ \t \t \t \t 6)1000€ \n")
			with open("kontostand.txt") as fin:
				for line in fin.readlines():
				rechnung_kontostand = int(zwischenschritt)
				if auslesen_kontostand == "":
					print("Sie haben kein Konto!")
					behebung = 0
					main = 1

			if auslesen_kontostand != "":
		
				if behebung_auswahl == "1" and rechnung_kontostand >= -2250:
					ergebniss_kontostand = rechnung_kontostand - 50
					ergebniss_kontostand2 = str(ergebniss_kontostand)
					print("Entnehmen Sie Ihre 50€")
					auslesen_kontostand = open("kontostand.txt", "w")
					with open("kontostand.txt", "w") as f:
						f.write(ergebniss_kontostand2)
						main = 1

		
	if auswahl == "5":
		main = 0
		print("Bis zum nächsten mal!")

	if auswahl != "1" and auswahl != "2" and auswahl != "3" and auswahl != "4" and auswahl != "5":
		print("Bitte geben Sie eine gültige Operation ein!")
		main = 1
Now it gives me that error:

File "bankomat.py", line 19
rechnung_kontostand = int(zwischenschritt)
^
IndentationError: expected an indented block
Reply
#9
Yes, when you have a function definition, loops, conditions, a with statement, a class definition, all of these expect an indented block of code. It could be just a line. Line 19 after the for loop and perhaps all the if statement block must be indented
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#10
Look always one or more lines before the error occurs.
Look in line 18
for line in fin.readlines():
After this line, you've to indent your code. The code inside the indented block is executed inside the for loop.

A little hint: In line 20 you're checking if rechnung_kontostand is an empty string. The if statement is checking for a boolean.
Mostly all types does have a good implementation for boolean.
>>> bool('') # empty string
False
>>> bool(' ') # non empty string
True
>>> bool('0')
True
>>> bool(0)
False
>>> bool(0.0)
False
>>> bool(1)
True
>>> bool(-1)
True
>>> bool(None)
False
>>> bool([]) # empty list
False
>>> bool([0]) # non empty list
True
>>> bool({}) # empty dict
False
>>> bool({'A': 'B'}) # non empty dict
True
>>> bool({'A'}) # non empty set
True
>>> bool(set()) # empty set
False
So in line 20 your check can be:

if auslesen_kontostand:
    # code

# or more fault tolerant
if auslesen_kontostand.strip():
    # code
# strip removes white spaces on the left and right side of a string.
# a string with only white spaces inside will return an empty string.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Last record in file doesn't write to newline gonksoup 3 404 Jan-22-2024, 12:56 PM
Last Post: deanhystad
  Recommended way to read/create PDF file? Winfried 3 2,871 Nov-26-2023, 07:51 AM
Last Post: Pedroski55
  write to csv file problem jacksfrustration 11 1,512 Nov-09-2023, 01:56 PM
Last Post: deanhystad
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 1,430 Nov-09-2023, 10:56 AM
Last Post: mg24
Question Special Characters read-write Prisonfeed 1 612 Sep-17-2023, 08:26 PM
Last Post: Gribouillis
  read file txt on my pc to telegram bot api Tupa 0 1,106 Jul-06-2023, 01:52 AM
Last Post: Tupa
  parse/read from file seperated by dots giovanne 5 1,105 Jun-26-2023, 12:26 PM
Last Post: DeaD_EyE
  Formatting a date time string read from a csv file DosAtPython 5 1,253 Jun-19-2023, 02:12 PM
Last Post: DosAtPython
  How do I read and write a binary file in Python? blackears 6 6,514 Jun-06-2023, 06:37 PM
Last Post: rajeshgk
  Read csv file with inconsistent delimiter gracenz 2 1,196 Mar-27-2023, 08:59 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020