1

I have a dictionary with parameters and their values which I retrieve from the console with an interactive program.

mydic = { 'n': Nvalue , 'p': Pvalue }

I want to fixe one value and vary the other between a min and max with a step. For example if the user chooses to vary p he must choose the min and max value and the step. And I must have at the and as dictionaries as possibilities.

min = 0.2
max = 0.5
step = 0.2
n = 3

as a result I will have :

mydic = { 'n': 3 , 'p': 0.2 }
mydic = { 'n': 3 , 'p': 0.4 }

Here is the code I have written. I didn't manage to update the dictionary to obtain the diffrent possibilities

if var_param == param["name"]:
   minimum = int(input("  Choose the min value of " + var_param + " : "))
   maximum = int(input("  Choose the max value of " + var_param + " : "))
   step = int(input("  Choose the step of the variation : "))

   i = minimum
   while i <= maximum :
       mydic.update({var_param: i+step})
       i= i + step

if var_param != param["name"]:
   mydic.update({param["name"]: int(input("Choose the value of "+ param["name"]+ " : "))})
print(mydic)

I get only one dictionary. I know that the problem is in the while-loop but I don't know how to fix it to get all the possible dictionaries with the right values.

Thanks in advance

4
  • Why not just store 'p':range(min, max, step)? Commented Dec 19, 2012 at 11:28
  • If I have floats does it work ? I thought that range works only with int Commented Dec 19, 2012 at 11:30
  • 1
    you are always doing mydic.update({var_param: i+step}) in the while, your old value is being replaced with the new one in update, since the key is same Commented Dec 19, 2012 at 11:30
  • have a look at stackoverflow.com/questions/477486/… Commented Dec 19, 2012 at 11:33

1 Answer 1

2

You are replacing your old dictionary inside the while loop. You need to assign a array [minimum, step, step, step, maximum] as the value for the dict key.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.