【发布时间】:2019-05-16 11:40:22
【问题描述】:
我不能让我的 self.points 改变我的积分变量。每次玩家再次轮到他/她时,它只会重新启动用户点回到 0。
该程序最少 2 人,最多 4 人。
您有 2 个随机掷出的六面骰子。如果玩家获得双打,则将两个骰子相加,然后乘以 2。答案将添加到分数中。如果第一个骰子是 1,则将两个骰子相加,然后从玩家点数中减去,结束回合。
如果玩家没有得到双倍或第一个骰子没有得到 1,则将两个骰子相加,然后添加到玩家分数。这种情况一直持续到玩家达到 100 分,游戏结束。
import random, sys
class game():
def __init__(self, name):
self.name = name
self.stop = False
def mainloop(self):
print("Would you like to roll the dice {}?: ".format(self.name))
return input().lower().startswith('y')
def playing(self, points, score):
self.points = points
self.score = score
while True:
if self.mainloop() == True:
print("it's {} go at the game with {} points".format(self.name, self.points))
self.rule()
if self.points >= 100:
print("the winners is {} with {} many points".format(self.name, self.points))
sys.exit()
print("{} your points is: {}".format(self.name, self.points))
if self.stop == True:
print("###############################################################")
print("##################### NEXT PLAYERS TURN #####################")
print("###############################################################")
self.score.append(self.points)
return (self.score)
else:
self.score.append(self.points)
return (self.score)
def rule(self):
question = 0
answer = 0
dice_1 = random.randint(1, 6)
dice_2 = random.randint(1, 6)
print("dice 1 is: {}".format(dice_1))
print("dice 2 is: {}".format(dice_2))
if dice_1 == dice_2:
if dice_1 != 1:
question = "({} + {}) * 2".format(dice_1, dice_2)
answer = eval(question)
print("add {} POINTS!!!".format(answer))
question = ("{} + {}".format(self.points, answer))
self.points = eval(question)
print(self.points)
elif dice_1 == 1:
print("add 25 POINTS!!!")
question = ("{} + 25".format(self.points))
self.points = eval(question)
print(self.points)
elif dice_1 == 1 or dice_2 == 1:
print("unluckly! minus the points")
question = "{} + {}".format(dice_1, dice_2)
answer = eval(question)
print("{} - {}".format(self.points, answer))
question = ("{} - {}".format(self.points, answer))
self.points = eval(question)
print(self.points)
self.stop = True
elif dice_1 != 1 or dice_2 != 1:
print("adding points!!!")
question = "{} + {}".format(dice_1, dice_2)
answer = eval(question)
print("{} + {}".format(self.points, answer))
question = "{} + {}".format(self.points, answer)
self.points = eval(question)
print(self.points)
return True
def check_letter(question):
if question.isalpha():
return False
else:
print("please input a letter")
return True
def main():
number = [2, 3, 4]
players = 0
while players not in number:
players = int(input("how many players are there? please input a number between 2 and 4: "))
score = []
username = []
for x in range(int(players)):
name = input("what is your name?: ")
while check_letter(name) == True:
name = input("what is your name? please input a letter: ")
username == username.append(name)
for z in range(int(players)):
score.append(0)
while True:
for user in username:
for points in score:
while True:
gamer = game(user)
gamer.playing(points, score)
break
break
main()
【问题讨论】:
-
在您与我们共享的代码中,您从未向
self.points写过任何内容。所以他们会一直保持在0。 -
另一点你的方法/属性(不确定你想要什么)
self.false是空的。你创建一个方法,然后分配false(不管那是因为它不是pythonsFalse)。 -
你需要将
game.playing的返回值存储在某处。 -
与其在评论部分发布您的帖子,不如编辑您的帖子。在 cmets 中无法正确格式化
-
您发布的代码对我有用(它不会停留在 100 分,但可能您需要将
==100与>= 100交换)
标签: python