2017-10-19 50 views
-1

所以我想学习Python,并且编写了一个基本上与用户和计算机一起玩游戏的程序。我的程序在循环中产生print语句无穷大。不知道如何解决这个问题

这个游戏的机制是:

有两名球员:用户和计算机,谁轮流交替,直到其中一个达到100分以上。

•用户始终是第一位玩家。

•如果任何玩家在回合结束时达到100分或更多,则游戏立即结束,另一位玩家不会得到另一回合。

•一回合包括以下内容:玩家掷出一个6面骰子。
o如果他们掷出1,他们得1分,并且他们轮到了。即使一名玩家拥有前一次掷骰的累计点数,如果他们掷出1,他们的回合得分将为1分,并且他们的回合结束。

o如果他们滚动任何其他数字,滚动分数将被添加到转数总和中。

o然后,他们可以选择继续滚动或保持。玩家在一回合中可以掷出多少次没有限制。

•持有:如果一名玩家持有,则他们获得当前回合总数并且结束。例如,如果玩家掷出3,4和2,然后决定持有,则他们得到9分。

•如果玩家用户,给他们的选择,他们是否愿意
继续滚动或每次滚动模和没有得到1

•如果时间后抱玩家是电脑,他们会一直继续滚动,直到他们轮到
达到10或更高的值。

代码的问题是代码运行时会产生无限的打印语句。我把它放在主函数中的布尔语句中,我设置了is_user_turn = True。我已经对代码进行了排序,但有些东西我没有看到,需要修复它。

下面是代码:

import random 

def welcome(): 
    print("Welcome to Jeopordy") 

def print_current_player(is_user_turn): 

    if (is_user_turn == True): 
     print("It is now human's turn") 
    if (is_user_turn == False): 
     print("It is now computer's turn") 

def roll_die(): 

    roll = random(1,6) 
    return roll 

def take_turn(is_user_turn,Computer_Hold): 

    turn_total = 0 

    if(is_user_turn == True): 
     while(is_user_turn == True): 
      roll = roll_die() 

     if(roll == 1): 
      return 1 
     turn_total = turn_total + roll 
     print("Your turn total is 1") 
     is_user_turn = False 

    else: 
     print("You rolled a ",roll) 
     turn_total = turn_total + roll 
     print("Your turn total is ",turn_total) 
     play = input("Do you want to roll gain (Y/N)?") 

    if(play == 'N' or play == 'n'): 
     is_user_turn = False 
     return turn_total 

    else: 
     is_user_turn == True 

    if(is_user_turn == False): 

      while(is_user_turn == False): 
       roll = roll_die() 

      while(turn_total <= Computer_Hold): 
       if(roll + turn_total <= Computer_Hold): 
        print("You rolled a ", roll) 
        turn_total = turn_total + roll 
        print("Your turn total is ",turn_total) 

    return turn_total 

def report_points(userscore,computerscore): 
    print("computer: ",computerscore) 
    print("user: ",userscore) 

def get_next_player(is_user_turn): 
    if(is_user_turn == True): 
     is_user_turn = False 
     return is_user_turn 
    else: 
     is_user_turn = True 
     return is_user_turn 

def main(): 

    Game_End_Points = 100 
    Computer_Hold = 10 
    is_user_turn = True 
    userscore = 0 
    computerscore = 0 

    welcome() 
    while(userscore <= Game_End_Points and computerscore <= Game_End_Points): 
     print_current_player(is_user_turn) 

     if(get_next_player(is_user_turn) == True): 
      userscore = userscore + take_turn(is_user_turn,Computer_Hold) 
      report_points(userscore,computerscore) 
      get_next_player(is_user_turn) 
     elif(get_next_player(is_user_turn) == False): 
      computerscore = computerscore + take_turn(is_user_turn,Computer_Hold) 
      report_points(userscore,computerscore) 
      get_next_player(is_user_turn) 

main() 
+0

欢迎来到StackOverflow。请阅读并遵守帮助文档中的发布准则。 [最小,完整,可验证的示例](http://stackoverflow.com/help/mcve)适用于此处。在发布您的MCVE代码并准确描述问题之前,我们无法为您提供有效的帮助。 我们应该能够将发布的代码粘贴到文本文件中,并重现您描述的问题。 – Prune

+0

我们不需要所有背景和整个程序。请减少你的问题。你已经缩小了问题 - 太棒了!只需发布该部分! – Blorgbeard

+0

你在while循环后8行的缩进中只是有一个错误。它们应该是while循环的一部分。 – justhalf

回答

1

这一部分是错误:

while(userscore <= Game_End_Points and computerscore <= Game_End_Points): 
     print_current_player(is_user_turn) 

这个执行此功能无限:

def print_current_player(is_user_turn): 

    if (is_user_turn == True): 
     print("It is now human's turn") 
    if (is_user_turn == False): 
     print("It is now computer's turn") 

,因为你的功能不改变userscore或computerscore,它被卡在那里。这是我现在的暗示。如果您需要进一步的帮助,请发表评论。 添加提示:“缩进” 也,只是检查你的整个代码 - 似乎还有其他错误以及:)

+0

据我了解,这个函数需要结束才能停止打印这条语句。我不完全确定如何做到这一点。也许我可以用柜台? – dezz

+0

另外,修正了压痕。 – dezz

+0

您可以使用计数器,但这不是您想要在该行上实现的目标,对吗?不要考虑首先结束循环,但应该执行下一步。那是你的第一步。 – jabargas

0

我不能告诉肯定,但看起来这可能会导致循环或至少会造成一些问题,为您提供:

if(play == 'N' or play == 'n'): 
    is_user_turn = False 
    return turn_total 

else: 
    is_user_turn == True 

在else语句中,您正在检查is_user_turn是否为True,而不是将其指定为True值。看起来应该是is_user_turn = True

+0

我修复了它,谢谢你的收获! – dezz

1

变量userscore总是小于Game_End_Points等都将成为计算机的分数,因此将无限循环,使用一些柜台的循环。

while(userscore <= Game_End_Points and computerscore <= Game_End_Points): 
    print_current_player(is_user_turn) 
    userscore=userscore+10 #something like this 
    computerscore+=10