-2

I am very new in python. i need someone to help me out with running a simple script in python. I have a code that I have attached along. I am new with defining functions and running if else functions so I need help on how to make the following function work?

I do not know if defining different functions is necessary and I know I must be wrong at many lines in the code below. I need someone to help me revise it

def values():
    p1g = float(input("Player 1 Utility for green"))
    p2r = float(input("Player 2 ulitity for red"))
    p1r = float(input("Player 1 utility for red"))
    p2g = float(input("Player 2 utility for green"))
    d1 = float(input("Player 1 Disagreement point"))
    d2 = float (input("Player 2 Disagreement point"))

    return [p1g,p2r,p14,p2g,d1,d2]

def slope_int(p1g,p2r,p1r,p2g):
   """Returns the slope and intercept of the payoff/utilities"""
   m = (p2g-p2r)/(p1r-p1g)
   c = p2r - ((p2g-p2r)/(p1r-p1g))* p1g
   return [m,c]

def nash_bargaining_x(p1g,p2r,p1r,p2g,d1,d2,m,c):
    return -(p2r -d2 - m*p1g - m*d1)/2*m

def nash_bargaining_y(p1g,p2r,p1r,p2g,d1,d2,m,c):
    return -(p2r -d2 - m*p1g - m*d1) + c # where c = p1g-p2r*((p2g-p2r)/(p1r-p1g)

solution = [nash_bargaining_x(),nash_bargaining_y()]
print (solution)
if abs.(p1g-nash_bargaining_x) > (p1r-nash_bargaining_x):
   solution = "P1 has Red"
else:
   solution = "P2 has red"

expect the program to calculate all the values as listed in the script and return the values required from the user input variables.

L3n95
  • 1,505
  • 3
  • 25
  • 49
  • 1
    Thank you. would you be able to help me with the script. Thank you – Saurabh Bikram Basnet Jul 10 '19 at 16:43
  • 1
    Where are you actually calling `values()` function? You've only defined it, not tried to run it – OneCricketeer Jul 10 '19 at 16:44
  • 1
    I do not know where to go from there. like i said I am really new in python. So the values () function i guess is not necessary? I just set it so that I would be able to define the variables p1g,p2r etc. I realize that 50 percent of the code doesnot make sense. i just wrote what i could. i do not know how to make this script run and give me back results .. thanks – Saurabh Bikram Basnet Jul 10 '19 at 16:46
  • @roganjosh I question is described above i guess. I just want the program to get values from user that are defined in the values() function, then use all the values to calculate the other defined functions like slope_int, nash_bargaining etc. and then return me the two solution – Saurabh Bikram Basnet Jul 10 '19 at 16:49
  • @blorgbeard I do not think i have said " I do not know how to start"? I have started it, but I dont know how to move forward from here. where are the mistakes in the script. I want the program to run some calcuations from user given inputs in the values() function, calcualte the slope_int(p1g,p2r,p1r,p2g),nash_bargaining_x,nash_bargaining_y, and give me the solution – Saurabh Bikram Basnet Jul 10 '19 at 16:52

2 Answers2

0

Defining Functions is not necessary in this code. However, if you do define a function like so:

def foo():
   …

then you have to call it again to make it run like so:

foo()

If you do not do that, then the function will not run.

Taking the values function, I would do

p1g,p2r,p14,p2g,d1,d2 = values()

before I would call slope_int(). Also, slope_int() needs to have the return tabbed, same with the nash_x and the nash_y. Then, I would call everything in order, right before the solutions thing, like so:

p1g,p2r,p14,p2g,d1,d2 = values()
m,c = slope_int(p1g,p2r,p1r,p2g)
solution = [nash_bargaining_x(),nash_bargaining_y()]
print(solution)
if abs.(p1g-nash_bargaining_x) > (p1r-nash_bargaining_x):
    solution = "P1 has Red"
else:
    solution = "P2 has red"
print(solution)

I guess this is what you are looking for

However, I would suggest NOT defining functions for this script. Functions should be defined if they will be called multiple times.

  • 1
    Thanks. I believe this is helpful. So where exactly I would have to call the defined function in order for it to run in the above script? – Saurabh Bikram Basnet Jul 10 '19 at 16:53
  • 1
    In this case you would want to call `values()` before any line that would need the values. In your code, right before the line ` solution = [nash_bargaining_x(),nash_bargaining_y()]` after all your function definitions. Unfortunately you will run into global variable issues afterwards, since your variables are only defined inside your function. I would SUGGEST for this case that you simply get rid of the function altogether and get the inputs as part of the main program (ie remove the lines `def values():` and `return [p1g,p2r,p14,p2g,d1,d2]` while un-indenting everything in between) – Hoog Jul 10 '19 at 17:01
  • Also, if you believe this answer is helpful, check the tick mark, and mark the answer as accepted. If you do not like it, down mark it, if you like it, up mark it. Just some advice on using this interface –  Jul 10 '19 at 17:07
  • 1
    Thank you guys. @MathWiz thank you for the revisions. actually I am defining functions as I want to put this script in a simulation platform and run it multiiple times. Also in your revised code, before the solution = nash_bargaining....etc, do i need to define these functions as I have in my code or could i just do give define it as a variable? – Saurabh Bikram Basnet Jul 10 '19 at 17:08
  • I believe you could just define it as a variable. Not sure what you mean though –  Jul 10 '19 at 17:13
  • Can I just write it as nash_bargaining_x = -(p2r -d2 - m*p1g - m*d1)/2*m – Saurabh Bikram Basnet Jul 10 '19 at 17:19
0

Ok, lets break this down as you have a number of issues in your way of having functional code. First off, you have a nice function values() to get all the input from the user but you don't ever use this function. You will need to call this function AND save all the outputs from it like so:

def values():

    p1g = float(input("Player 1 Utility for green"))
    p2r = float(input("Player 2 ulitity for red"))
    p1r = float(input("Player 1 utility for red"))
    p2g = float(input("Player 2 utility for green"))
    d1 = float(input("Player 1 Disagreement point"))
    d2 = float (input("Player 2 Disagreement point"))

    return [p1g,p2r,p14,p2g,d1,d2]


[p1g,p2r,p14,p2g,d1,d2] = values()

Now you have your variables from the user ready to use throughout your code. After that, your other function definitions look good, though they are indented poorly (but that's probably just you not being experienced with Stack Overflow formatting):

def slope_int(p1g,p2r,p1r,p2g):
       """Returns the slope and intercept of the payoff/utilities"""
       m = (p2g-p2r)/(p1r-p1g)
       c = p2r - ((p2g-p2r)/(p1r-p1g))* p1g

    return [m,c]

def nash_bargaining_x(p1g,p2r,p1r,p2g,d1,d2,m,c):

    return -(p2r -d2 - m*p1g - m*d1)/2*m

def nash_bargaining_y(p1g,p2r,p1r,p2g,d1,d2,m,c):
    return -(p2r -d2 - m*p1g - m*d1) + c # where c = p1g-p2r*((p2g-p2r)/(p1r-p1g)

Even though you have named the variables the same in these above function definitions and your input, you will not be able to just call these functions and expect them to have the variables from your values() function. You will have to explicitly send the numbers you want these functions to use in as arguments like so:

solution = [nash_bargaining_x(p1g,p2r,p1r,p2g,d1,d2,m,c),nash_bargaining_y(p1g,p2r,p1r,p2g,d1,d2,m,c)]
print (solution)

Now your solution should be as you expect, one final note is that the abs function for absolute values is not called with abs.() but abs() like so:

 if abs(p1g-nash_bargaining_x) > abs(p1r-nash_bargaining_x)
    solution = "P1 has Red"
    else
    solution = "P2 has red"
Hoog
  • 2,280
  • 1
  • 14
  • 20
  • Thank you @Hoog. This looks so much better. you are right I am new to this interface as well as python thus it is written poorly and I appreciate your effort. In the function slope_int where i have return [m,c] is this a proper way to write it so that the function will calculate m,c and I can use this variable anytime i want it? – Saurabh Bikram Basnet Jul 10 '19 at 17:16
  • I get the following error when i tried to run it File "", line 20 return [m,c] ^ IndentationError: unindent does not match any outer indentation level – Saurabh Bikram Basnet Jul 10 '19 at 17:20
  • That error will come up a lot as you get used to Python. In Python the indentation is of critical importance, a line with whitespace at the start is not the same as a line without. Your error means that on line 20 you have too many or too few spaces / tabs at the start of that line. Since that line is a continuation of your function `slope_int()` you will want the same number of tabs / spaces before it as the line above `c = p2r - ((p2g-p2r)/(p1r-p1g))* p1g`. That way python knows that your return line belongs in your function and should only be performed at the end of your function. – Hoog Jul 10 '19 at 17:36
  • Thank you for the compliment, but I don't really want to be used as a direct contact right now. I worry that if I let people contact me directly, coming here will become like a chore to me instead of an adventure for me to find the perfect question to answer. Just keep asking your tough Python questions on Stack Overflow, I'll keep an eye out for them :D – Hoog Jul 10 '19 at 18:01