【问题标题】:Beginner python game: Craps, while-loop issue初学者python游戏:掷骰子,while循环问题
【发布时间】:2020-07-24 11:35:11
【问题描述】:

我是编程新手,我的任务是制作一个掷骰子游戏。游戏继续进行,直到您在被要求再次玩时回答“否”。一旦你输入“否”,它应该显示你赢了和输了多少次。它工作得很好(不记分):

import random

def main():

    playGame = input("Would you like to play Craps? (Enter yes or no): ")
    while playGame == 'yes':
        roll = input("Press Enter to roll the dice")
        rollDice1 = random.randint(1, 6)
        rollDice2 = random.randint(1, 6)
        print("You got a", rollDice1, "and a", rollDice2)
        rolledDice = rollDice1 + rollDice2
        print("you rolled a", rolledDice)
        if rolledDice == 7 or rolledDice == 11:
            print("IT'S YOUR LUCKY DAY! YOU WIN!")

        elif rolledDice == 2 or rolledDice == 3 or rolledDice == 12:
            print("YOU LOSE! BETTER LUCK NEXT TIME!")

        else:
            print("YOU NEITHER WIN NOR LOSE!")

        playGame = input("Try again? (Enter yes or no): ")
        if playGame == "no":
            print("Place holder")
main()

当我试图保持得分时,当你输赢时它不会循环。 (虽然当你不赢也不输时它仍然循环):

import random

def main():
    wins = 0
    losses = 0

    playGame = input("Would you like to play Craps? (Enter yes or no): ")
    while playGame == 'yes':
        roll = input("Press Enter to roll the dice")
        rollDice1 = random.randint(1, 6)
        rollDice2 = random.randint(1, 6)
        print("You got a", rollDice1, "and a", rollDice2)
        rolledDice = rollDice1 + rollDice2
        print("you rolled a", rolledDice)
        if rolledDice == 7 or rolledDice == 11:
            print("IT'S YOUR LUCKY DAY! YOU WIN!")
            wins = wins + 1
            return wins

        elif rolledDice == 2 or rolledDice == 3 or rolledDice == 12:
            print("YOU LOSE! BETTER LUCK NEXT TIME!")
            losses = losses + 1
            return losses

        else:
            print("YOU NEITHER WIN NOR LOSE!")

        playGame = input("Try again? (Enter yes or no): ")
        if playGame == "no":
            print("Wins: ", wins)
            print("Losses: ", losses)
main()

感谢您提供的任何帮助和建议。就像我说的我是新手,所以请尝试以简单的方式解释什么是错误的以及我应该做什么。

【问题讨论】:

  • 不要使用字符串作为布尔值。 Python 知道TrueFalse
  • 如果你return - 你离开了这个功能 - 你的游戏就结束了。删除这些行,它将起作用。
  • 非常相似——如果不是骗子的话:return-in-function-only-returning-one-value
  • 太棒了,谢谢。不知道return离开函数。

标签: python loops


【解决方案1】:

如果你 return 来自函数内部的某些东西,你会离开它 - 这就是你的 while 循环不起作用的原因:

def main():
    wins = 0
    losses = 0

    playGame = input("Would you like to play Craps? (Enter yes or no): ")
    while playGame == 'yes':
        roll = input("Press Enter to roll the dice")
        rollDice1 = random.randint(1, 6)
        rollDice2 = random.randint(1, 6)
        print("You got a", rollDice1, "and a", rollDice2)
        rolledDice = rollDice1 + rollDice2
        print("you rolled a", rolledDice)
        if rolledDice == 7 or rolledDice == 11:
            print("IT'S YOUR LUCKY DAY! YOU WIN!")
            wins = wins + 1
            return wins   # EXITS main() - simply delete this row

        elif rolledDice == 2 or rolledDice == 3 or rolledDice == 12:
            print("YOU LOSE! BETTER LUCK NEXT TIME!")
            losses = losses + 1
            return losses # EXITS main() - simply delete this row

        else:
            print("YOU NEITHER WIN NOR LOSE!")

        playGame = input("Try again? (Enter yes or no): ")
        if playGame == "no":
            print("Wins: ", wins)
            print("Losses: ", losses)
             return  # add this to exit the function (could use break as well)
main()

【讨论】:

  • 谢谢,我删除了返回,它又循环了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-28
  • 2020-02-15
  • 1970-01-01
  • 2015-12-24
相关资源
最近更新 更多