Sep-15-2018, 09:09 PM
(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.