pythontraffic-simulation

I need the following script to run in python and give me result? Solving simple mathematical questions


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.


Solution

  • 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"