Python Forum
Making a number list in a .txt file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Making a number list in a .txt file
#2
(Sep-15-2018, 08:59 PM)kwak86 Wrote:
f=open("dico.txt","w")
a=0
while a<=999999:
	if a<=99999:
		def add_zero(nbr: str, m: int = 6) -> str:
			return "0" * (m - len(nbr)) + nbr
	f.write(str(a))
	a=a+1
	else:
    f.write(str(a))
	a=a+1
f.close()
A syntax error in the "else" line appears.

Could you tell me why?

Because your indents are off - but as I understand, you are trying to write 0-padded values to a file, so you define a function for it (in a very inefficient way, I must add) and then you never call it.

The proper - and much easier way would be to use formatting - if you number is called number - one-letter names is a bad practice - f'{number:06d} will do the trick in Python versions 3.6 and above. Up to 3.5, '{:06d}'.format(number) will do.
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply


Messages In This Thread
Making a number list in a .txt file - by kwak86 - Sep-15-2018, 08:59 PM
RE: Making a number list in a .txt file - by volcano63 - Sep-15-2018, 09:09 PM
RE: Making a number list in a .txt file - by kwak86 - Sep-15-2018, 09:39 PM
RE: Making a number list in a .txt file - by kwak86 - Sep-15-2018, 10:48 PM
RE: Making a number list in a .txt file - by kwak86 - Sep-16-2018, 04:12 PM
RE: Making a number list in a .txt file - by kwak86 - Sep-16-2018, 05:00 PM
RE: Making a number list in a .txt file - by kwak86 - Sep-16-2018, 05:34 PM
RE: Making a number list in a .txt file - by kwak86 - Sep-16-2018, 09:54 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Delete strings from a list to create a new only number list Dvdscot 8 1,865 May-01-2023, 09:06 PM
Last Post: deanhystad
  find random numbers that are = to the first 2 number of a list. Frankduc 23 3,883 Apr-05-2023, 07:36 PM
Last Post: Frankduc
Photo Making Zip file of a file and Directory Nasir 2 1,160 Oct-07-2022, 02:01 PM
Last Post: Nasir
  TypeError: float() argument must be a string or a number, not 'list' Anldra12 2 5,185 Jul-01-2022, 01:23 PM
Last Post: deanhystad
  Split a number to list and list sum must be number sunny9495 5 2,667 Apr-28-2022, 09:32 AM
Last Post: Dexty
  Divide a number by numbers in a list. Wallen 7 8,356 Feb-12-2022, 01:51 PM
Last Post: deanhystad
  When did the number got included in the list? Frankduc 14 3,466 Feb-03-2022, 03:47 PM
Last Post: Frankduc
Question Help to find the largest int number in a file directory SalzmannNicholas 1 1,748 Jan-13-2022, 05:22 PM
Last Post: ndc85430
Question Making a copy list in a function RuyCab 1 1,904 Jul-11-2021, 02:06 PM
Last Post: Yoriz
Smile Help making number analysis program Dainer 2 1,884 Jun-24-2021, 09:55 PM
Last Post: jefsummers

Forum Jump:

User Panel Messages

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