剪刀石头布游戏的计分器
我正在尝试在这个石头剪刀布的游戏中添加一个得分计数器。我希望能得到一些帮助。
import random
import math
fox = random.randint(0,900)
print '*' * 50
print ' ' * 10, 'Welcome To Rock Paper Scissors!'
print '*' * 50
opener = raw_input('DO YOU THINK YOU HAVE WHAT IT TAKES!!!')
#opener2 =
zero = int(0)
num_rounds_won = zero
def thrower1(fox):
fox = random.randint(0,900)
if fox <= 300:
return 'r'
elif 300 < fox <= 600:
return 's'
else:
return 'p'
rd1 = thrower1(fox)
while True:
choose = str(raw_input("Rock, Paper, or Scissors?"))
choice = choose.lower()[0:1]#
raw_input("play again?")
if yes ref()
def ref(choice, rd1):
if choice == 'r' and rd1 == 'p':
return num_rounds_won + 1
elif choice == 'p' and rd1 == 'r':
return num_rounds_won + 1
elif choice == 's' and rd1 == 'p':
return num_rounds_won + 1
elif choice == rd1:
return "Draw"
else:
return "I Win!"
results = ref(choice, rd1)
print results
print num_rounds_won + 1
3 个回答
0
我也简单看了一下这个问题。我不太确定你具体想问什么,但有很多方法可以记录分数。在你的情况下,最简单的办法就是创建一个包含三个元素的列表,比如说 [0, 0, 0]。每当用户赢了、输了或者平局时,你就往列表中的某个位置加一。
我还提供了一种不同的方法,使用一个类来跟踪游戏的进展。这个类可以很容易地修改,以满足你特定游戏所需的用户体验。
from random import choice
class RockPaperScissors(object):
def __init__(self):
print('{0}\nWelcome to Rock Paper Scissors!\n{0}'.format('*' * 30))
self.score = {'wins':0, 'losses':0, 'draws':0}
self.choices = {'r':'Rock', 'p':'Paper', 's':'Scissors'}
self.winningCombos = (('Rock','Scissors'), ('Paper','Rock'), ('Scissors','Paper'))
self.errors = 0
def newGame(self):
while True:
user = str(input('Choose Rock, Paper, or Scissors ')).lower()
if (len(user) < 1 or user[0] not in self.choices):
self.errors += 1
if (self.errors > 3):
return
continue
break;
self.errors = 0
machine = choice(list(self.choices.keys()))
results = self.whoWins(self.choices[user], self.choices[machine])
self.score[results] += 1
def whoWins(self, userChoice, machineChoice):
if (userChoice == machineChoice):
result = 'draws'
print("It's a draw...\n")
else:
result = 'wins' if (userChoice, machineChoice) in self.winningCombos else 'losses'
winMsg = 'Congrats! {0} beats {1}\n'.format(userChoice, machineChoice)
losMsg = 'Better luck next time...{1} beats {0}\n'.format(userChoice, machineChoice)
print(winMsg if result == ['wins', 1] else losMsg)
return result
def whatsTheScore(self):
results = [(k,v) for k, v in self.score.items()]
results.sort(reverse=True)
for k,v in results:
print('...{0:<6}: {1:>3}'.format(k, v))
if __name__ == '__main__':
game = RockPaperScissors()
result = input('Do you want to play a game? enter y or n ')
if result.lower() == 'y':
game.newGame()
while True:
result = input('Do you want to play again? enter y or n ')
if result.lower() != 'y':
break
game.newGame()
print('Final score is:')
game.whatsTheScore()
print('Thanks for playing!!')
else:
print('Please play again! Later!!')
0
在不知道你具体问题的情况下,我只能随便猜测一下,但我看到了一些需要修正的地方。
首先,了解Python是一种脚本语言很重要。它是逐行读取代码并执行的。如果你在定义一个函数之前就去调用它,程序会报错(这在像Java这样的编译语言中是不会出现的)。所以,建议你把所有的函数定义放在文件的顶部(在导入的部分下面)。这样做有两个好处:1)所有的定义都会在一开始就处理好,你就不用担心了;2)所有的函数都在一个地方,容易找到,而不是夹在代码中间。
你在定义ref()
之前就尝试使用它,这会导致错误。而且,你的ref()
函数需要两个参数(choice
和rd1
),但你调用它时没有传任何参数,这也是一个错误。
另一个问题是,你的while True
循环永远不会退出,所以在这个循环下面写的任何代码都不会被Python读取或执行。你需要把这个循环放在代码的最后部分(或者添加一个break
语句)。
0
我帮你模拟了一下你的代码(你在定义ref()之前就试图调用它了!),希望这能对你有所帮助。
import random
import math
fox = random.randint(0,900)
print '*' * 50
print ' ' * 10, 'Welcome To Rock Paper Scissors!'
print '*' * 50
opener = raw_input('DO YOU THINK YOU HAVE WHAT IT TAKES!!!')
#opener2 =
zero = int(0)
num_rounds_won = zero
def thrower1(fox):
fox = random.randint(0,900)
if fox <= 300:
return 'r'
elif 300 < fox <= 600:
return 's'
else:
return 'p'
def ref(choice, rd1):
if choice == 'r' and rd1 == 'p':
print "you won"
return 1
elif choice == 'p' and rd1 == 'r':
print "you won"
return 1
elif choice == 's' and rd1 == 'p':
print "you won"
return 1
elif choice == rd1:
print "Draw"
return 0
else:
print "I Win!"
return 0
while True:
rd1 = thrower1(fox)
choose = str(raw_input("Rock, Paper, or Scissors?"))
choice = choose.lower()[0:1]#
num_rounds_won += ref(choice, rd1)
play_again = raw_input("play again(y/n)?")
if play_again == "y":
continue
else:
break
print "you won", num_rounds_won, "games"