首页 > 解决方案 > 如何让这个程序更详细,并在我玩完后告诉我胜率?

问题描述

到目前为止,这个掷骰子游戏在 python 中运行,但我需要在你玩完之后添加胜率。另外,我如何让玩家响应必须是“是”或“否”才能运行?无论如何它都会运行。有没有办法在这个程序中替换“中断”?

import random
first_roll = 0

def play():
    yesOrno = str(input('Would you like to play? '))
    yesOrno = yesOrno[0].lower()


    while yesOrno == 'y':
        throw_1 = random.randint(1,6)
        throw_2 = random.randint(1,6)
        total = throw_1 + throw_2


        if total == (2,12):
            yesOrno = str(input('You lost! Would you like to play again?'))
            yesOrno = yesOrno[0].lower()

        elif total == (7,11):
            yesOrno == str(input('You won! Would you like to play again?'))
            yesOrno = yesOrno[0].lower()

        else:
            first_roll == total

            while yesOrno == 'y':
                throw_1 = random.randint(1,6)
                throw_2 = random.randint(1,6)
                finalRoll = throw_1 + throw_2

                print('You rolled a',total)

                if total == 2 or 3 or 7 or 11 or 12:
                    yesOrno = str(input('You lost! Would you like to play again?'))
                    yesOrno = yesOrno[0].lower()
                    break

                elif total == first_roll:
                    yesOrno = str(input('You won! Would you like to play again?'))
                    yesOrno = yesOrno[0].lower()
                    break

                else:
                    yesOrno == 'y'

    print('Thanks for playing!')

play()

标签: pythonpython-3.xdice

解决方案


我修复并简化了您的一些代码。我为重复性任务创建了 2 个助手:

import random

def askPlayAgain(text=""):
    """Ask for y or n, loop until given. Maybe add 'text' before the message."""
    t = ""
    while t not in {"y","n"}:
        t = input("\n" + text + 'Would you like to play? [y/n]').strip()[0].lower()
    return t

def getSumDices(n=2,sides=6):
    """Get sum of 'n' random dices with 'sides' sides"""
    return sum(random.choices(range(1,sides+1),k=n)) # range(1,7) = 1,2,3,4,5,6 

并稍微修改了播放逻辑:

def play():
    first_roll = 0
    win = 0
    lost = 0

    yesOrno = askPlayAgain()

    while yesOrno == 'y':

        # dont need the individual dices - just the sum
        # you can get both rolls at once
        total = getSumDices()         
        print('You rolled a', total)

        if total in {2,12}:
            lost += 1
            yesOrno = askPlayAgain("You lost! ") 

        elif total in {7,11}:
            win += 1
            yesOrno == askPlayAgain("You won! ") 
        else:
            # remember last rounds result
            first_roll = total

            while True:
                total = getSumDices()         
                print('You rolled a', total)

                # this is kinda unfair, if you had 3 in your first round
                # you cant win the second round at all ...
                if total in {2,3,7,11,12}:
                    lost += 1
                    yesOrno = askPlayAgain("You lost! ")
                    break
                elif total == first_roll:
                    win += 1
                    yesOrno = askPlayAgain("You won! ")
                    break

    print('Thanks for playing!')
    print('{} wins vs {} lost'.format(win,lost))

play()

输出:

Would you like to play? [y/n]y
You rolled a 10
You rolled a 12

You lost! Would you like to play? [y/n]y
You rolled a 9
You rolled a 7

You lost! Would you like to play? [y/n]y
You rolled a 7

You won! Would you like to play? [y/n]y
You rolled a 6
You rolled a 10
You rolled a 3

You lost! Would you like to play? [y/n]y
You rolled a 8
You rolled a 9
You rolled a 11

You lost! Would you like to play? [y/n]n
Thanks for playing!
1 wins vs 4 lost 

推荐阅读