-1

beginer question: I often have to repeat the same line of code 10 times for 10 different variables (i read you can't or should not create variable dynamically)

I thought to help me once i written it once I could iterate with a find replace (I am currently doing it in word)

I wrote this but get tons of error when i input code like if console trying to execute the code. what am i missing? is it because i paste it i the console does it need to be imported from a ext file ?

LineOfCode = input("Enter the code you want to iterate the iteration variable need be 8")
for i in range(10):
    LineOfCode.replace("8",str(i))
    print(LineOfCode)
  • `with a find replace (I am currently doing it in word)` - there are quite a few free Python editors/IDE's. You should try those out rather than using word. – wwii Feb 06 '21 at 14:39
  • 1
    You can use dicts when you need something like "creating variables dynamically". – AKX Feb 06 '21 at 14:43
  • `get tons of error` - When posting a question about code that produces an Exception, always include the complete Traceback - copy and paste it then format it as code (select it and type `ctrl-k`). Welcome to SO. This isn't a discussion forum or tutorial. Please take the [tour] and take the time to read [ask] and the other links found on that page. – wwii Feb 06 '21 at 14:45
  • 1
    Yeah, Word most definitely isn't a good editor for programming. Even Notepad is better, in my opinion. You should use some simple and lightweight text editor, like Sublime Text, or a lightweight IDE, like Visual Studio Code. – ForceBru Feb 06 '21 at 14:47
  • Related: [String replace doesn't appear to be working](https://stackoverflow.com/questions/26943256/string-replace-doesnt-appear-to-be-working), – wwii Feb 06 '21 at 14:49
  • Not enough details, but i tried my best to help you – TheEagle Feb 06 '21 at 14:51
  • @wwii "rather than using word" - the OP might have meant not the program MS Word, but the word of a phrase ? In this context, the sense of it might be "i did not replace it in the code, but rather in a string". The OP should definitely ad more details. – TheEagle Feb 06 '21 at 14:53
  • Does [Why doesn't calling a Python string method do anything unless you assign its output?](https://stackoverflow.com/questions/9189172/why-doesnt-calling-a-python-string-method-do-anything-unless-you-assign-its-out) answer your question? – wwii Feb 06 '21 at 14:54
  • Thank you for the quick answer i realise now my question was not clear enough (i do use VS Code but to copy the same code multiple times just used the find replace functionality of word) – Nicolas Renaud Feb 07 '21 at 01:02

2 Answers2

0

If you want to run the same code on multiple variables, you can create the code as a string, and then use the builtin exec to execute it. Example:

a = 1
b = 2
c = 3
variables = ["a", "b", "c"]
for var in variables:
    exec("print(" + var + ")") # print the variable before modifying
    exec(var + " *= 2") # multiply the current variable by 2
    exec("print(" + var + ")") # print the variable after modifying

If this is not what you need, your question was not clear enough and i misunderstood it, you just have to provide more information on what excatly you need by adding a comment under your question, in which you first type @Programmer and then your text.

TheEagle
  • 5,808
  • 3
  • 11
  • 39
0

Your code is almost correct:

String.replace() returns a new string with the changes, doesn't change the existing string. So the code should be like:

LineOfCode = input("Enter the code you want to iterate the iteration variable need be 8")

for i in range(10):
    var = LineOfCode.replace("8", str(i))
    print(var)

The question isn't clear enough, so if this isn't what you wanted, edit the question.

Besides this, you can use Visual Code as an IDE, it'll make things much, much easier than Word.

About the errors, I think you might be trying to run the code directly in console. Instead of typing only the file name, type python file.py, assuming the file name is file. The file must be a python file (ending with .py), also it shouldn't be saved as a word file. It must be plain text.