python - 重新启动游戏和用户输入无效时,回合#计数器不会重置
问题描述
所以我不明白为什么当我重新启动游戏时我的计数器没有重置,而且我相信这与我没有构建器的事实有关?任何帮助都将受到高度评价!
import random
import sys
from termcolor import colored
class RPS():
def show_welcome():
print(colored("Welcome to RPS !!!", 'blue'))
print(colored("RULES:",'yellow'), colored("*", 'red'), "Rock Beats Scissors",colored("*", 'red'))
print(colored(" *", 'grey'),"Scissors beats Paper", colored("*", 'grey'))
print(colored(" *", 'magenta'), "Paper beats Rock", colored("*", 'magenta'))
print(colored("!---=== GOOD LUCK ===---! ", 'green'))
def round_number():
x = 10
user_input = ""
while True:
try:
user_input = int(input("# Number Of Rounds? [Max 10]: "))
except:
print("Invalid Input!")
RPS.round_number()
if user_input < 1 or user_input > x:
print("Max 10 rounds!")
rundnum = 0
RPS.round_number()
else:
return user_input
def restart():
user_input = ""
try:
user_input = input("Would you like to restart? [Y/n]")
except:
print("\nInvalid Input! ")
RPS.show_welcome()
if user_input is "y" or user_input is "Y":
rounds = 0
RPS.round_number()
elif user_input is "n" or user_input is "N":
print(colored("Thanks for playing! Goodbye!", 'green'))
sys.exit()
else:
print("Bullshit input!")
RPS.restart()
def game():
player = 0
rounds = 0
moves = ['Rock', 'Paper', 'Scissors',]
r = rundnum
while rounds < r:
rounds += 1
comp = random.choice(moves)
print(colored("Choose : ", 'green'), colored("1)Rock", 'red'), colored("2)Paper", 'yellow'),
colored("3)Scissors", 'grey'))
try:
player = int(input("What's Your Guess? "))
except:
print(colored("No Valid Input! Restarting Game...", 'red'))
RPS.game()
if (player is 1 and comp is 'Rock') or (player is 2 and comp is 'Paper') \
or (player is 3 and comp is 'Scissors'):
print("Player Choose: {}".format(moves[player - 1]))
print("Computer Choose: {}".format(comp))
print(colored("*** Round #: {} | Result: It's A Draw !! *** ", 'blue').format(rounds))
if rounds >= r:
print(colored("*** GAME OVER *** ", 'grey'))
rounds = 0
RPS.restart()
else:
continue
elif (player is 1 and comp is 'Paper') or (player is 2 and comp is 'Scissors') \
or (player is 3 and comp is 'Rock'):
print("Player Choose: {}".format(moves[player - 1]))
print("Computer Choose: {}".format(comp))
print(colored("*** Round #: {} | Result: Player Lose !! *** ", 'blue').format(rounds))
if rounds >= r:
print(colored("*** GAME OVER *** ", 'grey'))
rounds = 0
RPS.restart()
else:
continue
elif (player is 1 and comp is 'Scissors') or (player is 2 and comp is 'Rock') \
or (player is 3 and comp is 'Paper'):
print("Player Choose: {}".format(moves[player - 1]))
print("Computer Choose: {}".format(comp))
print(colored("*** Round #: {} | Result: Player Wins !! *** ", 'blue').format(rounds))
if rounds >= r:
print(colored("*** GAME OVER *** ", 'grey'))
rounds = 0
RPS.restart()
else:
continue
else:
print(colored("No valid input!", 'red'))
RPS.game()
if __name__ == '__main__':
#rounds = ""
while True:
RPS.show_welcome()
rundnum = RPS.round_number()
RPS.game()
rounds = 0
rundnum = 0
解决方案
当您RPS.game()
在函数本身内调用时会出现问题。这会产生一种情况,即所有变量都会按照函数的定义进行重置.game()
。为避免这种情况,您可以为函数提供默认参数,然后使用这些参数调用函数(在其内部)。
示例(使用部分原始代码):
def game(player=0, rounds=0):
# player = 0
# rounds = 0
moves = ['Rock', 'Paper', 'Scissors',]
r = rundnum
while rounds < r:
rounds += 1
comp = random.choice(moves)
print(colored("Choose : ", 'green'), colored("1)Rock", 'red'), colored("2)Paper", 'yellow'),
colored("3)Scissors", 'grey'))
try:
player = int(input("What's Your Guess? "))
except:
print(colored("No Valid Input! Restarting Game...", 'red'))
RPS.game(player, rounds) # Here's the magic...
推荐阅读
- flutter - 断言失败:第 50 行 pos 15:'image != null':在 Flutter 中不正确
- c# - 将 int 转换为 List
, ChartJS 核心 - php - Laravel 页面中的动态文本字段
- postgresql - 查询关联表PK上的多:多关系(即按关联表列过滤)
- c# - 使用 Newtonsoft 的 PopulateObject 函数时出现 JsonSerializationException
- python - ModuleNotFoundError:没有名为“code.victim”的模块;“代码”不是一个包
- javascript - 导出 PNG 画布所有 div 内容时出错
- java - 从 java admin sdk 连接到 Firestore 模拟器
- sql - 如何对数据组进行排序?
- ansible - Ansible:尝试使用 influxdb_write 时无法在 localhost 上导入所需的 Python 库(influxdb)