Sam*_*Man -2 python python-3.x
我已经阅读了一些全局变量,但我的代码不起作用.这是代码:
global ta
global tb
global tc
global td
ta = 1
tb = 1.25
tc = 1.5
td = 2
def rating_system(t1, t2):
global ta
global tb
global tc
global td
if t1 < t2 and t2/t1 <= 4:
rating = (t2/t1) * 0.25
t1 += rating
t2 -= rating
else:
rating = (t2/t1) * 0.4
t1 += rating
t2 -= rating
print(str(t1) + " and " + str(t2))
rating_system(ta, td)
Run Code Online (Sandbox Code Playgroud)
我给变量所有global定义,但是当我运行时rating_system(),它只是为变量打印正确的数字,但是如果我在函数外部打印变量,它会给我默认数字.
你的八global条线都没有在这个程序中做任何事情.目前尚不清楚,但我猜你要做的就是将两个数字传递给函数,并用函数的结果替换它们.在这种情况下,您需要做的就是return结果并在调用函数时重新分配它们:
def rating_system(t1, t2):
if t1 < t2 and t2/t1 <= 4:
rating = (t2/t1) * 0.25
t1 += rating
t2 -= rating
else:
rating = (t2/t1) * 0.4
t1 += rating
t2 -= rating
return (t1, t2)
(ta, td) = rating_system(ta, td)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
204 次 |
| 最近记录: |