所以我试图在这个岩石剪刀游戏上放一个计分器。我会喜欢一些帮助
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
答案 0 :(得分: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"
答案 1 :(得分:0)
在不知道你的问题是什么的情况下,这是一个完整的黑暗镜头,但我看到一些我认为需要解决的问题。
首先,了解Python是一种脚本语言非常重要。它在运行时读取代码,一次一行。如果在定义函数之前尝试调用函数,那么我将是一个错误(在Java等编译语言中不是这种情况)。这就是为什么您应该将所有定义放在文件的顶部(在导入之下)。这有两个优点。 1)您的所有定义都是首先处理的,不需要担心它们。 2)所有功能都在一个地方,很容易找到,而不是卡在代码的中间。
在定义之前,您尝试使用ref()
。这将导致错误。此外,您的ref()
有2个参数(choice
和rd1
),但您可以不带参数调用它。这也是一个错误。
另一个问题是你的while True
循环永远不会退出,所以你在这个循环下面写的东西将永远不会被Python读取或运行。您需要将此循环作为代码的绝对最后一部分(或添加break
语句)
答案 2 :(得分: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!!')