我希望我的以下代码可以使用新值多次执行循环吗?

时间:2019-07-11 15:50:13

标签: python loops iteration

我在python中有一个代码。我希望它像循环一样运行多次。可以说20次。我会使用什么语法?

因此对于以下代码,我希望它进入循环并使函数倾斜多次,并提供结果。

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"))
"""Returns the slope and intercept of the payoff/utilities"""
i = 1
while i < 20:
 m = (p2g-p2r)/(p1r-p1g)
 c = p2r - ((p2g-p2r)/(p1r-p1g))* p1g
 nash_bargaining_x = -(p2r -d2 - m*p1g - m*d1)/2*m
 nash_bargaining_y = -(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) > abs(p1r-nash_bargaining_x):
     solution = "P1 has Red"
 else:
    solution = "P2 has red"
 print(solution)
 if solution in ["P1 has red"]:
  p1r = p1r + 25 and p1g = p1r - 20 and p2g = p2g - 15 and p2r = p2g + 35
 else:
  p1r = p1g + 25 and p1g = p1g -25 and p2g = p2r - 20 and p2r = p2r + 25

语法错误:无法分配给运算符

1 个答案:

答案 0 :(得分:0)

  

p1r = p1r + 25 and p1g = p1r - 20 and p2g = p2g - 15 and p2r = p2g + 35

您可能是故意的

if solution == "P1 has red":
  p1r = p1r + 25
  p1g = p1r - 20
  p2g = p2g - 15
  p2r = p2g + 35

({and是错误消息中的运算符;它用于计算布尔表达式,而不是加入一系列赋值运算符。)