我在让分数增加时遇到一些麻烦。它可以工作,但在第一个“+1”完成后它会一直重置为零。
user1score = 0
user2score = 0
def main():
if form.has_key('choice'):
myMove = form['choice'].value
cpuM = computerMove()
result = compareMove(myMove, cpuM)
now = datetime.datetime.now()
writeFile(user, result, now)
show(user)
print user
print now.strftime("%Y-%m-%d %H:%M")
print result
else:
show(user)
def computerMove():
cc = ["rock","scissor","paper"]
return random.choice(cc)
def compareMove(myMove, cpuM):
global user1score
global user2score
if myMove == cpuM:
return "Its a tie!"
elif myMove == 'rock':
if cpuM == 'paper':
user2score += 1
return "Paper beats Rock. Computer wins! " + str(user2score)
else:
user1score += 1
return "Rock beats Scissors. You win! " + str(user1score)
elif myMove == 'paper':
if cpuM == 'rock':
user1score += 1
return "Paper beats Rock. You win! " + str(user1score)
else:
user2score += 1
return "Scissors beats paper. Computer wins! " + str(user2score)
elif myMove == 'scissors':
if cpuM == 'rock':
user2score += 1
return "Rock beats scissors. Computer wins " + str(user2score)
else:
user1score += 1
return "Scissors beats paper. You win " + str(user1score)
如何在分数增加1后防止分数回归零?其他一切都按预期工作。
谢谢。
答案 0 :(得分:0)
由于每次用户执行操作时都会调用脚本,因此无法重新记录前一轮的结果。
使用某种会话(cookie)或将分数添加到表单作为隐藏元素。