Python中文
首页
教程
问答
标签
搜索
登录
注册
调用函数而不重置变量
回答此问题可获得
20
贡献值,回答如果被采纳可获得
50
分。
<p>我正在做一个纸牌游戏,当它结束时,我希望人们能够玩更多的回合,但当它重放时,它必须再次通过变量。。重置分数。我正在寻找一种方法来修复它,而不需要一个全新的复杂代码块,我希望我只是错过了一个非常简单的修复。在</p> <pre><code>#!/usr/bin/python # -*- coding: utf-8 -*- # Imports import time def play_game(): # Variables acard = int() bcard = int() apoints = int() bpoints = int() # Repeat repeat = True # Hand out cards print 'Cards have been served' input('\nPress Enter to Reveal') # Cards Turn time.sleep(0.5) t = time.time() acard = int(str(t - int(t))[2:]) % 13 print '\nYour card value is ' + str(acard) time.sleep(0.1) t = time.time() bcard = int(str(t - int(t))[2:]) % 13 # Number Check & Point Assign time.sleep(2) if acard > 5: apoints += 1 print '\nYour points have increased by one, your total is now ' \ + str(apoints) if acard < 5: apoints -= 1 print '\nYour points have decreased by one, your total is now ' \ + str(apoints) if bcard > 5: bpoints += 1 print '\nYour opponent got ' + str(bcard) \ + ', their points have increased by one,\ntheir total is now ' \ + str(bpoints) if bcard < 5: bpoints -= 1 print '\nYour opponent got ' + str(bcard) \ + ', their points have decreased by one,\ntheir total is now ' \ + str(bpoints) # Card Reset bcard = 0 acard = 0 # Shuffle input('\nPress enter to shuffle deck') print '\nCards being shuffled' time.sleep(1) print '.' time.sleep(1) print '.' time.sleep(1) print '.' print 'Cards have been shuffled' global enda global endb enda = apoints endb = bpoints # Loop time.sleep(1) answer = 'y' while answer.lower() == 'y': play_game() answer = input('\nDo you wish to play again? (Y/N)') # Scores if enda > endb: print '\nYou Win!' print '\nScores' print 'You: ' + str(enda) + ' Opponent: ' + str(endb) if endb > enda: print '\nYou Lost!' print '\nScores' print 'You: ' + str(enda) + ' Opponent: ' + str(endb) </code></pre>
0 条评论
分类:
Python问答
请先
登录
后评论
默认排序
时间排序
1 个回答
匿名
1天前
擅长:python、mysql、java
<p>我可能会使用面向对象的方法来编写类级别的变量,但是如果您想让代码尽可能接近现在的代码,可以将score变量移到方法之外:</p> <pre><code>apoints = 0 bpoints = 0 def play_game(): global apoints global bpoints ... </code></pre>
请先
登录
后评论
针对此问题:
更多的回答
关注
89
关注
收藏
1
收藏,
216
浏览
网友 提问于 2天前
相关Python问题
如何在PyObj中使用respondsToSelector和performSelector
8 回答
如何在pyobj中停止线程
1 回答
如何在pyobj中生成线程
1 回答
如何在pyodbc中为记录集指定游标类型?
4 回答
如何在pyodbc中从用户处获取表名,同时避免SQL注入?
5 回答
如何在pyodbc中使用executemany运行多个SELECT查询
5 回答
如何在pyodbc中同时在n个游标上并行运行n个进程?
9 回答
如何在pyodbc中控制连接池的大小?
8 回答
如何在pyodbc中自动调用fetchall()而不进行异常处理?
1 回答
如何在pyODBC查询中参数化日期戳?
7 回答
如何在pyodbc输出转换器函数中解压sqlserver DATETIME?
4 回答
如何在pyodb中安装所有驱动程序
2 回答
如何在pyodb嵌套循环中调用不同的查询
3 回答
如何在pyomo.environ公司modu装置
4 回答
如何在Pyomoconstraints中建模逻辑或量词
3 回答
如何在Pyomo中为约束使用数组
4 回答
如何在pyomo中使用集和范围集的多级索引?
5 回答
如何在PYOMO中分配伪二进制变量
7 回答
如何在Pyomo中创建OR约束?
4 回答
如何在Pyomo中动态地将变量添加到列表中?
6 回答