【发布时间】:2019-05-13 20:22:51
【问题描述】:
我不确定为什么变量totalspeed 变量没有正确传递给函数startgame,因为startgame 函数是在gettotalspeed 函数之后调用的。
调用函数的摘录:
gettotalspeed(party_ids)
NoOfEvents=0
startgame(party_ids,totalspeed,distance,NoOfEvents)
功能
def gettotalspeed(party_ids):
#Get selected party members IDS
print(party_ids)
#Obtain Speeds
ids_string = ','.join(str(id) for id in party_ids)
mycursor.execute("SELECT startspeed FROM characters WHERE CharID IN ({0})".format(ids_string))
myspeeds=mycursor.fetchall()
totalspeed=0
for speedval in myspeeds:
totalspeed=totalspeed + speedval[0]
print("totalspeed is: ",totalspeed)
return totalspeed
def startgame(party_ids,totalspeed,distance,NoOfEvents):
#Check if game end
print(totalspeed)
while distance!=0:
#Travel...
distance=distance-totalspeed
NoOfEvents=NoOfEvents+1
#Generate Random Encounter
genevent(NoOfEvents)
return NoOfEvents
产生的错误:
NameError: name 'totalspeed' is not defined
输出 (ignoring party_ids)
totalspeed is: 15
【问题讨论】:
标签: python function parameter-passing