用python编一个扔骰子猜大小的游戏,要求三局两胜制
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用python编一个扔骰子猜大小的游戏,要求三局两胜制相关的知识,希望对你有一定的参考价值。
,运行要求如下
Please input your guess(big/small): small
You are wrong.
Please input your guess(big/small): dfdfd
Please input your guess(big/small): big
You are right.
Please input your guess(big/small): big
You are wrong.
You lost this round
Play again?(y/n): ddd
Play again?(y/n): n
You played 1 rounds, and you won 0 rounds
123为小,456为大,求大神解决,谢谢
搜集的资料:
def game(w, l):
def winning():
print("You are right.")
again(w+1, l)
def losing():
print("You are wrong.")
again(w, l+1)
def again(w, l):
ans = input("Play again?(y/n)")
if ans == 'n':
print("You played %s rounds, and you won %s rounds" % (w+l, w))
elif ans == 'y':
game(w, l)
else:
again(w, l)
guess = input("Please input your guess(big/small): ")
dice = random.randrange(1, 7)
if guess == "small":
winning() if dice <= 3 else losing()
elif guess == "big":
winning() if dice >= 4 else losing()
else:
game(w, l)
if __name__ == '__main__':
game(0, 0) 参考技术B from random import choice
from time import sleep
go_on='y'
won=0
played=0
while go_on=='y':
right=0
for i in range(3):
inputed=''
while inputed!=='big' and inputed!=='small':
inputed=raw_input('Please input your guess(big/small):')
if inputed==choice['big','small']:
print 'You are right'
right+=1
else:
print 'You are wrong'
played+=1
if right>1:
won+=1
go_on=''
while go_on!='y' and go_on!='n':
go_on=raw_input('Play again?')
print 'You played',played,'rounds, and you won',won,'rounds.'
sleep(2) 参考技术C 参考我之前回答的问题:http://zhidao.baidu.com/question/982003419122883339.html?oldq=1
要求好像一样。追问
但是好像不是三局两胜的
追答在game函数体内定义一个变量i存储当前回合的小场胜负数,添加一个while循环就可以了。
试图用Python重新创建一个名为“ Going to Boston”的骰子游戏
我正在尝试用python开发“去波士顿”。代码中解释了很多规则,注释中也解释了很多代码。我会回答任何问题,但是我的输出出现了一些问题。这是我所拥有的。
# This code aims to recreate the game "Going to Boston" for four players.
# Rules:
# - Each player rolls three dice.
# – Each player keeps their highest die and sets it aside.
# – The remaining two dice are re-rolled and the highest of the two is set aside.
# – The last die is rolled and the final score is the sum of the three dice.
# ----------------------------------------------------------------------------------------------------------------------
# We'll use the random module for the dice.
import random
# This variable is just to count rounds
RoundCount = 0
# These global variables are being used to save the score of each player.
Score1 = 0
Score2 = 0
Score3 = 0
Score4 = 0
FinalScore = [] # We'll use this to decide the winner(s).
# Here is our class for player.
class Player:
def __init__(self, name):
self.name = name # Storing their name, just to more easily identify each player.
d1 = random.randint(1,6) # Randomly choosing a number from 1-6 to simulate a dice.
d2 = random.randint(1,6)
d3 = random.randint(1,6)
self.dice = [d1, d2, d3]
self.SavedDice = [] # We'll be using this variable to save dice.
def score(self):
# here is where dice are actually coded and saved.
# Here, we'll save the max value in dice.
self.SavedDice.append(max(self.dice))
self.dice.remove(max(self.dice)) # We're removing the max value, and maintaining the previous two dice.
for i in self.dice:
i = random.randint(1,6)
self.dice.append(i)
#return print(self.name,"\b's score is:",sum(self.SavedDice))
return(self.dice)
def __str__(self): # Here is where we return the saved dice.
return print(self.name,'Saved Dice:',self.SavedDice)
# Here is where we actually play.
# First, we setup the players.
Player1 = Player('Player 1')
Player2 = Player('Player 2')
Player3 = Player('Player 3')
Player4 = Player('Player 4')
# We'll use a loop to manage rounds.
while RoundCount < 3:
RoundCount += 1
# We use the __str__ method to show the currently saved dice.
Player1.__str__()
Player2.__str__()
Player3.__str__()
Player4.__str__()
# We'll assign values for scoring, to be used later, here
FScore1 = Player1.score()
FScore2 = Player2.score()
FScore3 = Player3.score()
FScore4 = Player4.score()
# Here is where we'll score each player.
# We'll first append all the final scores of each player to be compared.
FinalScore.append(FScore1, FScore2, FScore3, FScore4)
WinningScore = max(FinalScore)
if FScore1 == WinningScore:
print('Player 1 Won!')
if FScore2 == WinningScore:
print('Player 2 Won!')
if FScore3 == WinningScore:
print('Player 3 Won!')
if FScore4 == WinningScore:
print('Player 4 Won!')
# Just cleanly exiting the code.
print(' ')
exit('End of Game')
我最终得到这样的输出,但我不确定为什么。我还需要保留def str(self):并使用它。
Player 1 Saved Dice: []
Player 2 Saved Dice: []
Player 3 Saved Dice: []
Player 4 Saved Dice: []
似乎没有完全循环或以某种方式被取消,并且值没有保存到self.SavedDice
或SavedDice
。
您在以下块上无限循环:
for i in self.dice:
i = random.randint(1,6)
self.dice.append(i)
尝试用SavedDice
代替:
for i in self.dice:
i = random.randint(1,6)
self.SavedDice.append(i)
import random
# Here is our class for player.
class Player:
def __init__(self, name):
self.name = name # Storing their name, just to more easily identify each player.
d1 = random.randint(1,6) # Randomly choosing a number from 1-6 to simulate a dice.
d2 = random.randint(1,6)
d3 = random.randint(1,6)
self.dice = [d1, d2, d3]
self.saved_dice = [] # We'll be using this variable to save dice.
def roll(self):
# here is where dice are actually coded and saved.
# Here, we'll save the max value in dice.
max_dice = max(self.dice)
self.saved_dice.append( max_dice )
self.dice.remove( max_dice ) # We're removing the max value, and maintaining the previous two dice.
self.dice = [ random.randint(1,6) for _ in self.dice ] # recreate list with size of remained values
@property
def score(self):
return sum(self.saved_dice)
def __str__(self): # Here is where we return the saved dice.
return f'{self.name} Saved Dice: { self.saved_dice }'
def main():
max_players = 4
round_count = 0
# Create set of players
players = { Player(f'Player { i + 1 }') for i in range(max_players) }
while round_count < 3:
round_count += 1
for player in players:
# Roll each player per each round
player.roll()
print(player)
# Choosing winner player
winner = max( players, key=lambda p: p.score )
print(f'{winner.name} Won!')
print(' ')
exit('End of Game')
if __name__ == '__main__':
main()
以上是关于用python编一个扔骰子猜大小的游戏,要求三局两胜制的主要内容,如果未能解决你的问题,请参考以下文章