1

I have the following code but I can't seem to get the program working in Visual Studio. I need to configure the interpreter arguments within the build settings to get the program to work.

How do I configure the interpreter / script arguments in Visual Studio for this Python program?

import sys

def Fib(limit): 
    """
    Lager en liste med fibonacci tall
    """

    fib = [1]
    second_last = 0
    last = 1

    for i in range(limit - 1):
        second_last, last = last, last + second_last
        fib.append(last)

    return fib


if __name__ == "__main__":

    limit = int(sys.argv[1])

    if (limit < 0):
        print("Kan ikke bruke negative tall")
        sys.exit()

    fibs = Fib(limit)

    for elem in Fibs:
        #print (elem)
        sys.stdout.write("  {}   ".format(elem))

    sys.stdout.write("\n")
Alan Kavanagh
  • 9,425
  • 7
  • 41
  • 65
Larsern88
  • 13
  • 2
  • You need to configure your interpreter arguments in the settings of visual studio for when you run the code.. look for something like 'run configurations' and then enter a number, for example 1 – Alan Kavanagh Sep 27 '17 at 14:19
  • Check out [this](https://stackoverflow.com/questions/4818673/is-in-visual-studio-an-equivalent-to-the-eclipse-run-configurations) question for more help – Alan Kavanagh Sep 27 '17 at 14:20

1 Answers1

0

The reason why your code will not execute is because you have the line

limit = int(sys.argv[1])

When this line runs, the interpreter will check for the first parameter which was passed as an argument when executing the script

for example:

python fib.py 1
# sys.argv[1] = 1

python fib.py 2
# sys.argv[1] = 2

When you click 'run' in Visual Studio, by default this will run your script without parameters:

python fib.py
# sys.argv[1] = None

so you need to locate your settings. Locate these settings and update the configurations to include parameters with some numerical value. This should resolve your issue with the arguments

this link explains to do it as follows:

  1. Right-click the default project (the one to be run) in Visual Studio and select "Properties".
  2. Click on the "Debug" tab on the left.
  3. Enter your command line arguments in the textbox labeled "Interpreter Arguments".
  4. Save the updated properties and run the project.

in this answer it explains that the values can be entered in the settings within visual studio

Menu -> Build \ Configuration Manager -> New
Alan Kavanagh
  • 9,425
  • 7
  • 41
  • 65
  • Thank you for your response. I've tried finding "command line arguments" where it's refered to, but all I find are Search paths, script arguments, interpreter path, interpreter arguments and environment variables. Am I in the wrong window, or are those tips for an older version of visual studio, or am I just missing some addition? – Larsern88 Sep 27 '17 at 17:49
  • Script arguments or interpreter arguments – Alan Kavanagh Sep 27 '17 at 18:35
  • @Larsern88 which one was it? – Alan Kavanagh Sep 27 '17 at 19:24
  • Interpreter arguments. But I found you had to add the filename for it to work. – Larsern88 Sep 29 '17 at 08:10