Python 为什么代码的第一部分会重复(引言),而它应该在变量轮更改后只运行一次? import random,time def main(): AIMoney=0 AINumGen='0' PlayerMoney=0 圆形=1 PlayerChoice=“” Result=“” 如果舍入=1: print(“你因抢劫世界著名博物馆而入狱。你可以选择背叛(通过承认你的伴侣参与)你的伴侣,或者串通(坚持一个故事,所以你们都按照最初的意图保留这笔钱。)你的目标是在最后拿到最多的钱,把你从抢劫中得到的奖励放在一起。你将与A.I打10轮,试图击败他们,这是一个复杂的游戏。这意味着总共有四个结果,每个实例你都有两个选择:) 打印(“”) 打印(“如果你们两个共谋,你们两个都不会坐牢,并且你们两个都通过将你们赚的钱分成两半来获得奖励。如果你们两个在每一轮中都共谋,那么对你们两个来说总是平局。结果:+100美元每一个。”) 打印(“”) 打印(“如果双方都背叛对方,双方都不会受到惩罚,但双方都不会从中得到任何好处。双方都可以获得+0分。在游戏结束时获得0分或负分将导致失败。”) 打印(“”) 打印(“如果你串通,但另一个人背叛了你,你将受到拿走所有贵重物品的人的严厉惩罚。因为你投入金钱,甚至最初实施抢劫,你在这种情况下会赔钱,并因你的罪行而被投入监狱。”) 打印(“”) 如果四舍五入=10: 打印(“游戏已结束。最终结果:”) 打印(“AI的钱:+AIMoney”) 打印(“您的钱:+PlayerMoney”) 出口 用户=输入(“C类与你的合作伙伴串通,B类背叛你的合作伙伴。”) 如果用户==“C”: AINumGen==random.randint(1,10) 圆形=圆形+1 如果AINumGen“4”: 结果=结果+“与您共谋。” 打印(“您选择与您的合作伙伴串通,他们”+结果) 打印(“这一轮后你的钱:+str(PlayerMoney)) 如果用户==“B”: 结果=结果 圆形=圆形+1 打印(“你选择背叛你的伴侣,他们”+结果) 打印(“这一轮后你的钱:+str(PlayerMoney)) 如果用户==“调试”: main() 如果是圆的!=10: 用户=输入(“C类与你的合作伙伴串通,B类背叛你的合作伙伴。”) main() 其他: 打印(“无效输入”) main()

Python 为什么代码的第一部分会重复(引言),而它应该在变量轮更改后只运行一次? import random,time def main(): AIMoney=0 AINumGen='0' PlayerMoney=0 圆形=1 PlayerChoice=“” Result=“” 如果舍入=1: print(“你因抢劫世界著名博物馆而入狱。你可以选择背叛(通过承认你的伴侣参与)你的伴侣,或者串通(坚持一个故事,所以你们都按照最初的意图保留这笔钱。)你的目标是在最后拿到最多的钱,把你从抢劫中得到的奖励放在一起。你将与A.I打10轮,试图击败他们,这是一个复杂的游戏。这意味着总共有四个结果,每个实例你都有两个选择:) 打印(“”) 打印(“如果你们两个共谋,你们两个都不会坐牢,并且你们两个都通过将你们赚的钱分成两半来获得奖励。如果你们两个在每一轮中都共谋,那么对你们两个来说总是平局。结果:+100美元每一个。”) 打印(“”) 打印(“如果双方都背叛对方,双方都不会受到惩罚,但双方都不会从中得到任何好处。双方都可以获得+0分。在游戏结束时获得0分或负分将导致失败。”) 打印(“”) 打印(“如果你串通,但另一个人背叛了你,你将受到拿走所有贵重物品的人的严厉惩罚。因为你投入金钱,甚至最初实施抢劫,你在这种情况下会赔钱,并因你的罪行而被投入监狱。”) 打印(“”) 如果四舍五入=10: 打印(“游戏已结束。最终结果:”) 打印(“AI的钱:+AIMoney”) 打印(“您的钱:+PlayerMoney”) 出口 用户=输入(“C类与你的合作伙伴串通,B类背叛你的合作伙伴。”) 如果用户==“C”: AINumGen==random.randint(1,10) 圆形=圆形+1 如果AINumGen“4”: 结果=结果+“与您共谋。” 打印(“您选择与您的合作伙伴串通,他们”+结果) 打印(“这一轮后你的钱:+str(PlayerMoney)) 如果用户==“B”: 结果=结果 圆形=圆形+1 打印(“你选择背叛你的伴侣,他们”+结果) 打印(“这一轮后你的钱:+str(PlayerMoney)) 如果用户==“调试”: main() 如果是圆的!=10: 用户=输入(“C类与你的合作伙伴串通,B类背叛你的合作伙伴。”) main() 其他: 打印(“无效输入”) main(),python,debugging,Python,Debugging,我也非常感谢其他方面的帮助,因为变量轮似乎从未改变过——即使代码运行了九次左右以获得游戏结果,它也不会做任何事情。我感觉它与操作顺序有关。由于您再次调用main()函数,变量Round正在重新初始化,这就是为什么要在那里调用该部件 不确定其他变量,但您需要检查每次调用条件时需要初始化什么 尝试将变量排除在主函数之外以下是正在发生的情况的简短分解: 调用main()并设置Round=1 你只玩一次游戏 如果是圆的!=10(现在是),再次打印问题,然后调用main() main()再次将Round设

我也非常感谢其他方面的帮助,因为变量轮似乎从未改变过——即使代码运行了九次左右以获得游戏结果,它也不会做任何事情。我感觉它与操作顺序有关。

由于您再次调用
main()
函数,变量
Round
正在重新初始化,这就是为什么要在那里调用该部件

不确定其他变量,但您需要检查每次调用条件时需要初始化什么


尝试将变量排除在主函数之外

以下是正在发生的情况的简短分解:

  • 调用main()并设置Round=1
  • 你只玩一次游戏
  • 如果是圆的!=10(现在是),再次打印问题,然后调用main()
  • main()再次将Round设置为1,使循环不确定

  • 您有两种选择:

  • 如果你总是有10个回合的话,那就做一个for循环。10轮结束后,您可以显示结果
  • 如果你可以有不确定的轮数,就选择递归。这里要做的更改是将整数作为参数传递给函数。如果回合数为1,请打印说明,如果您想结束游戏,请将特定数字(如10)传递给函数

  • 每次在主功能(1)中初始化时,您的输入是什么?我们需要一个,没有输入就不能有一个。预期产出也至关重要。(2) 整个节目的导言应该只打印一次吗(标题不清楚)?(3) 为什么不使用while循环呢?
    import random, time
    def main():
      AIMoney = 0
      AINumGen = '0'
      PlayerMoney = 0
      Round = 1
      PlayerChoice = ""
      Result = ""
      if Round == 1:
          print ("You are in prison for the robbing of a world famous museum. You have the choice of either betraying (by confessing your partner's involvement) your partner, or colluding. (sticking to a story, so you both keep the money as initially intended.) Your goal is to get the most money by the end, keeping your reward from the heist together. You wil play ten rounds against an A.I, attempting to beat them and this tricky game. This means there are four results in total, and you both have two options per instance:")   
          print ("")     
          print ("If both of you collude, you both stay out of prison, and you both gain a reward by splitting the amount of money you make in half. If you both collude the entire time throughout every round, it will always end in a tie for both of you. Result: +100 money for each of you.")
          print ("")
          print("If both of you betray each other, you both receive no penalty, but neither of you get anything out of it. +0 money for both of you. Having 0 or negative points at the end of the game will result in a loss.")
          print("")
          print ("If you collude, but the other person betrays you, you will be punished severely by the person taking all of the valuables. Since you put in money to even commit the heist initally, you lose money in this situation, and are put into prison for your crimes.")
          print("")
    
      if Round == 10:
        print("The game has ended. Final Result:")
        print("AI's Money:" + AIMoney)
        print("Your Money:" + PlayerMoney)
        exit
      User = input("Type C to collude with your partner, and B to betray your partner.")
      if User == 'C':
        AINumGen == random.randint(1, 10)
        Round = Round + 1
        if AINumGen < '4':
          Result = Result + "betrayed you."
          print ("You chose to collude with your partner and they " + Result)
          PlayerMoney = PlayerMoney - 200
        elif AINumGen > '4':
          Result = Result + "colluded with you."
          print("You chose to collude with your partner and they " + Result)
          print("Your money after this round: " + str(PlayerMoney))
      if User == 'B':
        Result = Result
        Round = Round + 1
        print("You chose to betray your partner and they" + Result)
        print("Your money after this round: " + str(PlayerMoney))
      if User == 'Debug':
        main()
      if Round != 10:
          User = input("Type C to collude with your partner, and B to betray your partner.")
          main()
      else:
        print("Invalid Input")
    main()