What is wrong with my code?
Dave Angel
d at davea.name
Sun Oct 23 18:56:34 EDT 2011
On 10/23/2011 06:03 AM, apometron wrote:
> import os
> nome = sys.argv[1]
> final = nome
> for i in nome:
> print i
> if nome[i] = "_":
> final[i] = " "
> os.rename(nome, final)
>
What do you want to be wrong with it? There are so many things, it'd be
fun to try to see who could come up with the most.
1) it's not a valid Fortran program.
2) it's missing a shebang line
if we assume it's for Windows, or that you run it with an explicit
bash line
3) if we pretend it's a python program, a few more
3a) It has a syntax error calling the print() function. (Python 3.2)
If we assume it's a python 2.x program
4) it uses sys, without importing it
5) it uses second argument without checking if the user typed such an
argument
6) it tries to change a character within a string, which is a
non-mutable type
7) It creates two more references to the same string sys.argv[1], then
tries to modify one of them, not realizing the others would change to.
8) it tries to subscript a string using a character.
9) it calls rename with two references to the same object. So nothing
will ever actually happen, even if the other problems were fixed.
Generally, you'll get the best answers here if you specify more of your
environment (python version, OS), show what you tried (pasted from the
command line), and the results you got (such as stack traces).
HTH
DaveA
--
DaveA
More information about the Python-list
mailing list