【问题标题】:need help determining why output is always leonard wins all games需要帮助确定为什么输出总是伦纳德赢得所有比赛
【发布时间】:2018-04-29 19:13:59
【问题描述】:
#!/usr/bin/python3

# Constants - These constants will help improve the readability of your code
SHELDON = "Sheldon"
LEONARD = "Leonard"

LEONARD_WINS = 0
SHELDON_WINS = 1
TIE = 2

ROCK = 0
PAPER = 1
SCISSORS = 2
LIZARD = 3
SPOCK = 4

# Variables - Use these variables in your code to track the program's progress
leonardsShape = 0
sheldonsShape = 0 
roundsToPlay = 0

leonardsWinCount = 0
sheldonsWinCount = 0
tieCount = 0

def convertShapeToText(shape):
    choices = {ROCK : "Rock", PAPER : "Paper", SCISSORS : "Scissors", LIZARD : "Lizard", SPOCK : "Spock"}
    return choices.get(shape, "default")

# =======================================================================================================
# DO NOT MODIFY any of the code above this point
# ========================================================================================================


# It's OK to change these constants (use the shape names above - ROCK, SCISSORS, PAPER, LIZARD, SPOCK)
# See the Initial Values in the provided examples
SHELDONS_FIRST_SHAPE = ROCK 
LEONARDS_FIRST_SHAPE = ROCK
NUMBER_OF_ROUNDS = 4
print ("Jason Bolling")
print ("project 4: Rock Paper Scissors Lizard Spock")
def determineLeonardsNextShape(roundOutcome) :
    global leonardsShape
    global sheldonsShape
    if leonardsShape == 0 :
      if roundOutcome == 0 :
        return determineLeonardsNextShape == 0
      elif roundOutcome == 1 :
        if sheldonsShape == 4 :
          return determineLeonardsNextShape == 3
        elif sheldonsShape == 3 :
          return determineLeonardsNextShape == 0
        elif sheldonsShape == 0 :
          return determineLeonardsNextShape == 1
        elif sheldonsShape == 1 :
          return determineLeonardsNextShape == 2
        elif sheldonsShape == 2 :
          return determineLeonardsNextShape == 4
      elif roundOutcome == 2 :
        if leonardsShape == 0 :
          return determineLeonardsNextShape == 1
        elif leonardsShape == 2 :
          return determineLeonardsNextShape == 2
        elif leonardsShape == 2 :
          return determineLeonardsNextShape == 4
        elif leonardsShape == 3 :
          return determineLeonardsNextShape ==  0
        elif leonardsShape == 4 :
          return determineLeonardsNextShape == 3
    elif leonardsShape == 1 :
      if roundOutcome == 0 :
        return determineLeonardsNextShape == 1
      elif roundOutcome == 1 :
        if sheldonsShape == 4 :
          return determineLeonardsNextShape == 3
        elif sheldonsShape == 3 :
          return determineLeonardsNextShape == 0
        elif sheldonsShape == 0 :
          return determineLeonardsNextShape == 1
        elif sheldonsShape == 1 :
          return determineLeonardsNextShape == 2
        elif sheldonsShape == 2 :
          return determineLeonardsNextShape == 4
      elif roundOutcome == TIE :
        if leonardsShape == 0 :
          return determineLeonardsNextShape == 1
        elif leonardsShape == 1 :
          return determineLeonardsNextShape == 2
        elif leonardsShape == 2 :
          return determineLeonardsNextShape == 4
        elif leonardsShape == 3 :
          return determineLeonardsNextShape ==  0
        elif leonardsShape == 4 :
          return determineLeonardsNextShape == 3
    elif leonardsShape == 2 :
      if roundOutcome == 0 :
        return determineLeonardsNextShape == 2
      elif roundOutcome == 1 :
        if sheldonsShape == 4 :
          return determineLeonardsNextShape == 3
        elif sheldonsShape == 3 :
          return determineLeonardsNextShape == 0
        elif sheldonsShape == 0 :
          return determineLeonardsNextShape == 1
        elif sheldonsShape == 1 :
          return determineLeonardsNextShape == 2
        elif sheldonsShape == 2 :
          return determineLeonardsNextShape == 4
      elif roundOutcome == 2 :
        if leonardsShape == 0 :
          return determineLeonardsNextShape == 1
        elif leonardsShape == 1 :
          return determineLeonardsNextShape == 2
        elif leonardsShape == 2 :
          return determineLeonardsNextShape == 4
        elif leonardsShape == 3 :
          return determineLeonardsNextShape ==  0
        elif leonardsShape == 4 :
          return determineLeonardsNextShape == 3
    elif leonardsShape == 3 :
      if roundOutcome == 0 :
        return determineLeonardsNextShape == 3
      elif roundOutcome == 1 :
        if sheldonsShape == 4 :
          return determineLeonardsNextShape == 3
        elif sheldonsShape == 3 :
          return determineLeonardsNextShape == 0
        elif sheldonsShape == 0 :
          return determineLeonardsNextShape == 1
        elif sheldonsShape == 1 :
          return determineLeonardsNextShape == 2
        elif sheldonsShape == 2 :
          return determineLeonardsNextShape == 4
      elif roundOutcome == 2 :
        if leonardsShape == 0 :
          return determineLeonardsNextShape == 1
        elif leonardsShape == 1 :
          return determineLeonardsNextShape == 2
        elif leonardsShape == 2 :
          return determineLeonardsNextShape == 4
        elif leonardsShape == 3 :
          return determineLeonardsNextShape ==  0
        elif leonardsShape == 4 :
          return determineLeonardsNextShape == 3
    elif leonardsShape == 4 :
      if roundOutcome == 0 :
        return determineLeonardsNextShape == 4
      elif roundOutcome == 1 :
        if sheldonsShape == 4 :
          return determineLeonardsNextShape == 3
        elif sheldonsShape == 3 :
          return determineLeonardsNextShape == 0
        elif sheldonsShape == 0 :
          return determineLeonardsNextShape == 1
        elif sheldonsShape == 1 :
          return determineLeonardsNextShape == 2
        elif sheldonsShape == 2 :
          return determineLeonardsNextShape == 4
      elif roundOutcome == 2 :
        if leonardsShape == 0 :
          return determineLeonardsNextShape == 1
        elif leonardsShape == 1 :
          return determineLeonardsNextShape == 2
        elif leonardsShape == 2 :
          return determineLeonardsNextShape == 4
        elif leonardsShape == 3 :
          return determineLeonardsNextShape ==  0
        elif leonardsShape == 4 :
          return determineLeonardsNextShape == 3
      return

def determineSheldonsNextShape(roundOutcome):
    global leonardsShape
    global sheldonsShape
    if roundsToPlay == {2, 4} :
      return sheldonsShape == 4
    elif sheldonsShape == 0 :
      if roundOutcome == 1 :
        return determineSheldonsNextShape == 0
      elif roundOutcome == 0 :
        return determineSheldonsNextShape == 1
      elif roundOutcome == 2 :
        return determineSheldonsNextShape == 3
    elif sheldonsShape == 1 :
      if roundOutcome == 1 :
        return determineSheldonsNextShape == 0
      elif roundOutcome == 0 :
        return determineSheldonsNextShape == 1
      elif roundOutcome == 2 :
        return determineSheldonsNextShape == 3
    elif sheldonsShape == 3 :
      if roundOutcome == 1 :
        return determineSheldonsNextShape == 0
      elif roundOutcome == 0 :
        return determineSheldonsNextShape == 1
      elif roundOutcome == 2 :
        return determineSheldonsNextShape == 3
    elif sheldonsShape == 4 :
      if roundOutcome == 1 :
        return determineSheldonsNextShape == 0
      elif roundOutcome == 0 :
        return determineSheldonsNextShape == 1
      elif roundOutcome == 2 :
        return determineSheldonsNextShape == 3
    elif sheldonsShape == 2 :
      if roundOutcome == 1 :
        return determineSheldonsNextShape == 0
      elif roundOutcome == 0 :
        return determineSheldonsNextShape == 1
      elif roundOutcome == 2 :
        return determineSheldonsNextShape == 3
    return

def determineWinner():
    global leonardsShape
    global sheldonsShape
    if sheldonsShape == 0 and leonardsShape == 0 :
      return determineWinner == TIE
    elif sheldonsShape == 0 and leonardsShape == 1 :
      return determineWinner == LEONARD_WINS
    elif sheldonsShape == 0 and leonardsShape == 2 :
      return determineWinner == SHELDON_WINS
    elif sheldonsShape == 0 and leonardsShape == 3 :
      return determineWinner == SHELDON_WINS
    elif sheldonsShape == 0 and leonardsShape == 4 :
      return determineWinner == LEONARD_WINS
    elif sheldonsShape == 1 and leonardsShape == 0 :
      return determineWinner == SHELDON_WINS
    elif sheldonsShape == 1 and leonardsShape == 1 :
      return determineWinner == TIE
    elif sheldonsShape == 1 and leonardsShape == 2 :
      return determineWinner == LEONARD_WINS
    elif sheldonsShape == 1 and leonardsShape == 3 :
      return determineWinner == LEONARD_WINS 
    elif sheldonsShape == 1 and leonardsShape == 4 :
      return determineWinner == SHELDON_WINS 
    elif sheldonsShape == 2 and leonardsShape == 0 :
      return determineWinner == LEONARD_WINS
    elif sheldonsShape == 2 and leonardsShape == 1 :
      return determineWinner == SHELDON_WINS
    elif sheldonsShape == 2 and leonardsShape == 2 :
      return determineWinner == TIE
    elif sheldonsShape == 2 and leonardsShape == 3 :
      return determineWinner == SHELDON_WINS
    elif sheldonsShape == 2 and leonardsShape == 4 :
      return determineWinner == LEONARD_WINS
    elif sheldonsShape == 3 and leonardsShape == 0 :
      return determineWinner == LEONARD_WINS
    elif sheldonsShape == 3 and leonardsShape == 1 :
      return determineWinner == SHELDON_WINS
    elif sheldonsShape == 3 and leonardsShape == 2 :
      return determineWinner == LEONARD_WINS
    elif sheldonsShape == 3 and leonardsShape == 3 :
      return determineWinner == TIE
    elif sheldonsShape == 3 and leonardsShape == 4 :
      return determineWinner == SHELDON_WINS
    elif sheldonsShape == 4 and leonardsShape == 0 :
      return determineWinner == SHELDON_WINS
    elif sheldonsShape == 4 and leonardsShape == 1 :
      return determineWinner == LEONARD_WINS
    elif sheldonsShape == 4 and leonardsShape == 2 :
      return determineWinner == SHELDON_WINS
    elif sheldonsShape == 4 and leonardsShape == 3 :
      return determineWinner == LEONARD_WINS
    elif sheldonsShape == 4 and leonardsShape == 4 :
      return determineWinner == TIE
    return

def getBetterShape(shape):
  if shape == 0 :
    return getBetterShape == 1
  elif shape == 1 :
    return getBetterShape == 2
  elif shape == 2 :
    return getBetterShape == 4
  elif shape == 3 :
    return getBetterShape == 0
  elif shape == 4 :
    return getBetterShape == 3
  return
 # =======================================================================================================
# DO NOT MODIFY any of the code below this point
# =======================================================================================================       
def displayResults():
    print("{0}'s initial move: {1}\n{2}'s initial move: {3}\n\n".format(SHELDON, convertShapeToText(SHELDONS_FIRST_SHAPE), LEONARD, convertShapeToText(LEONARDS_FIRST_SHAPE)))
    if sheldonsWinCount > leonardsWinCount:
        print("{0} wins! ".format(SHELDON))
    elif leonardsWinCount > sheldonsWinCount:
        print("{0} wins! ".format(LEONARD))
    else:
        print("Tie Game!")
    if not sheldonsWinCount == leonardsWinCount:
        print("{0} won {1} game(s), {2} won {3} game(s), and they tied {4} game(s)\n".format(SHELDON, sheldonsWinCount, LEONARD, leonardsWinCount, tieCount))
    else:
        print("{0} and {1} each won {2} game(s) and tied {3} game(s)\n".format(SHELDON, LEONARD, sheldonsWinCount, tieCount))

def playGame():
    global leonardsShape, sheldonsShape

    for x in range(0, roundsToPlay):
        outcome = determineWinner()
        updateScores(outcome)
        leonardsShape = determineLeonardsNextShape(outcome)
        sheldonsShape = determineSheldonsNextShape(outcome)

def updateScores(roundOutcome):
    global leonardsWinCount, sheldonsWinCount, tieCount

    if roundOutcome == SHELDON_WINS:
        sheldonsWinCount = sheldonsWinCount + 1
    elif roundOutcome == LEONARD_WINS:
        leonardsWinCount = leonardsWinCount + 1
    else:
        tieCount = tieCount + 1

def main():
    print("Ima Python Programmer\nRock-Paper-Scissors-Lizard-Spock\n")
    playGame()
    displayResults()

leonardsShape = LEONARDS_FIRST_SHAPE
sheldonsShape = SHELDONS_FIRST_SHAPE
roundsToPlay = NUMBER_OF_ROUNDS

main() # This must be the LAST statement of the program (DO NOT INDENT)

所以输出总是 leonard 赢得每场比赛,无论到目前为止如何变化,这个输出都不会改变,而 Sheldon 从来没有赢得任何比赛。我想知道我在代码中哪里出错了。 我尝试将代码更改为“ROCK”、“SCISSORS”等以及数字表示,但代码仍然读取结果,因为 leonard 总是获胜

【问题讨论】:

  • 请将您的帖子缩短为相关代码。谢谢!
  • determineWinner 令人兴奋。除了你只忘记了一百万个其他有意义的组合
  • 你能举个例子吗?

标签: python python-3.x output


【解决方案1】:

您的问题在于代码的这些部分:

def determineLeonardsNextShape(roundOutcome):
  ...
  return determineLeonardsNextShape == 3
  ...

def determineSheldonsNextShape(roundOutcome):
  ...
  return determineSheldonsNextShape == 3
  ...

def determineWinner():
  ...
  return determineWinner == LEONARD_WINS
  ...

您正在返回函数与 int?
之间的比较 我认为你的意思是简单地返回一个特定的值。

您可以在playGameupdateScores 中追踪问题:

  • determineWinner 是一个函数,而LEONARD_WINS/SHELDON_WINS/TIE 都是ints(您可以使用type 或直接打印对象进行检查),所以您在determineWinner 中所做的所有比较将总是返回False
  • 然后updateScores总是False 调用
  • 然后roundOutcome始终False
  • roundOutcome == LEONARD_WINS始终True (False == 0)

因此,伦纳德总是赢。
同样的逻辑适用于determineLeonardsNextShapedetermineSheldonsNextShape,它们也总是返回False,并且会通过== 0 比较。

return <value>替换ALL那些return determine<something> == <value>

def determineLeonardsNextShape(roundOutcome):
  ...
  return 3
  ...

def determineSheldonsNextShape(roundOutcome):
  ...
  return 3
  ...

def determineWinner():
  ...
  return LEONARD_WINS
  ...

我认为在那之后你会得到想要的行为:

Jason Bolling
project 4: Rock Paper Scissors Lizard Spock
Ima Python Programmer
Rock-Paper-Scissors-Lizard-Spock

roundOutcome=1
roundOutcome=1
roundOutcome=0
roundOutcome=2
Sheldon's initial move: Spock
Leonard's initial move: Scissors

Sheldon wins! 
Sheldon won 2 game(s), Leonard won 1 game(s), and they tied 1 game(s) 

【讨论】:

    猜你喜欢
    • 2022-01-01
    • 2022-09-30
    • 2023-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-16
    • 2011-05-13
    相关资源
    最近更新 更多