所以我不明白为什么我的计数器不重置时,我重新开始游戏,而且,我相信这与我没有建设者的事实有关? 任何帮助将是高度赞赏!
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
问题来源StackOverflow 地址:/questions/59383812/round-counter-wont-reset-when-restarting-the-game-when-user-input-is-invalid
当您在函数本身中调用rpm .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...
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。