感谢大家在这里的帮助。我已经解决了我的代码问题。
答案 0 :(得分:2)
除了使用while True
之外,您还可以使用以下内容:
gamecount = 0
while (gamecount < 5):
print (f'play game {gamecount}')
#insert your other game code here
gamecount += 1
还要记住,按照@Eliad Cohen在回答中所说的,删除代码中的break
。
答案 1 :(得分:1)
乍一看,除非您将导入移动到第一行并缩进“ for”循环下的所有内容,否则此代码块将不会运行。您的代码现在正在执行的操作只是将随机模块导入五次。
另外,在第11行中使用之前,必须实例化scoreOne。
我同意@ycx注释,我认为使用while循环会更好。
我认为您在第57行中使用break
会使循环在您希望结束之前结束。您可以在http://www.tutorialspoint.com/python/python_loop_control.htm
希望到目前为止有帮助。
答案 2 :(得分:0)
正如其他答案所指出的那样,代码的缩进和排序已关闭。这可能是编程错误,也可能是将代码复制到堆栈溢出的结果。
除了缩进,脚本还有很多重复的代码。为了减少代码重复并使脚本更易于阅读和理解,您可能希望将程序分成不同的functions。
尽管我不打算废除您以前的作品,但这是我对游戏的实现(据我所了解),因此您可以了解一个人如何构建一个简单的脚本。我的确意识到有更优雅的方式来实现游戏,但是为了清楚起见,我决定坚持使用通用的代码构造。
#! /usr/bin/env python3
# imports
from random import randint
# functions
def roll_dice(n_dice):
"""Rolls specified number of dice and returns their values"""
# wait until the player types 'roll'
while True:
answer = input("Type 'roll' to roll the dice: ")
if answer.lower() == "roll":
break
# generate results using list comprehension
results = [randint(1, 6) for die in range(n_dice)]
print("you rolled: ", results)
return results
def play_turn(player_name):
"""Lets specified player play a turn and returns the resulting score"""
print(player_name, " it's your turn to play!")
# roll two dice
roll_one, roll_two = roll_dice(2)
print(type(roll_one))
score = roll_one + roll_two
# if it was a double, roll die no 1 again
if roll_one == roll_two:
print("You rolled a double!", "Roll again", sep="\n")
roll_one = roll_dice(1)[0]
score += roll_one
if roll_one % 2 == 0 or roll_two % 2 == 0: # if at least one die has an even value:
score += 10
else:
score -= 5
print("you scored", score, "points", "\n")
return score
def play_game(player_names):
"""runs the game, given a collection of player names"""
# set up a score board.
# # This can be implemented more elegantly, but this solution is most clear, I think.
# the alternative would be a dictionary comprehension: score_board = {player: 0 for player in player_names}
score_board = dict()
for player in player_names:
score_board[player] = 0
# do 5 rounds of regular playing, adding extra rounds if the game ties
round_no = 0
while True:
for player in player_names:
score = play_turn(player)
score_board[player] = max(0, score_board[player] + score) # make sure the score doesn't drop below 0
print("current scores are", score_board)
round_no += 1
# after 5 normal rounds have been played:
if round_no >= 5:
if len(set(score_board.values())) == 1: # if the game tied:
print("game tied. Let's play until someone wins!")
continue
else:
# determine who won
max_score = max(score_board.values())
for player in score_board:
if score_board[player] == max_score:
print(player, "won with a score of", max_score)
return # a return will exit the function and thus break the loop
def main():
"""The main function controls the entire program on a high level"""
player_names = ("player 1", "player 2")
play_game(player_names)
return
# These lines make sure the game won't start when this file is imported using 'import'
if __name__ == '__main__':
main()