保持最后一点

时间:2017-10-24 03:04:29

标签: python

我正在尝试编写一个程序,允许用户掷出2个骰子,并保持他们获得的点总数。

更新:非常感谢,我想出来了:)

此解决方案有效:

import random

def roll_dice():
    total = 0
    while(True):
        print ("Enter '1' to roll 2 dice, enter '2' to stop rolling")
        user_input = input()

        if user_input == '1':
            d1 = random.randint(1,6)
            d2 = random.randint(1,6)
            print (d1, d2)

            score = 0 # score this round
            if d1 == d2:
                score = 10
            elif d1 == 6 or d2 == 6: 
                score = 4
            elif d1 + d2 == 7:
                score = 7
            total += score # update total
            print ("The score this round is ", score)
            print ("The total number of points is", total)

        elif user_input == '2':
            print ("Thanks for playing!")
            break

roll_dice()

3 个答案:

答案 0 :(得分:1)

  1. 每次掷骰子时,你的行total = 0会将total重置为零。尝试将total = 0放在while循环之外。

  2. subtotal = sum(both_dice)此变量both_dice的目的是什么?为什么这是一个清单?在您的代码中,它只能包含一个数字或根本不包含任何数字。

  3. 这个程序应该有效。请仔细逐行理解该程序的工作原理。询问是否有任何不清楚的地方。

    import random
    
    def roll_dice():
        total = 0
        while(True):
            print ("Enter '1' to roll 2 dice, enter '2' to stop rolling")
            user_input = str(input()) # str() needed for Python 2
    
            if user_input == '1':
                d1 = random.randint(1,6)
                d2 = random.randint(1,6)
                print (d1, d2)
    
                score = 0 # score this round
                if d1 == d2:
                    score = 10
                elif d1 == 6 or d2 == 6: 
                    score = 4
                elif d1 + d2 == 7:
                    score = 7
    
                total += score # update total
                print ("The score this round is ", score)
                print ("The total number of points is", total)
    
            elif user_input == '2':
                print ("Thanks for playing!")
                break
    
    roll_dice()
    

答案 1 :(得分:0)

您可以这种方式编写代码。

import random

def roll_dice():
    total = 0
    while True:
        print ("Enter '1' to roll 2 dice, enter '2' to stop rolling")
        user_input = input()
        if user_input == 1:
            # Get die_one and die_two entry
            die_one, die_two  = random.randint(1,6), random.randint(1,6)
            print ("Got %d in die_one and %d in die_two" % (die_one, die_two))
            score = 0
            if die_one == die_two:
                score = 10
            elif die_one == 6 or die_two ==6:
                score = 4
            elif die_one + die_two == 7:
                score = 7

            print ("The current score is %d" % score)
            total += score
            print ("The total score is %d " % total)

        elif user_input == 2:
            print ("Thanks for playing!")
            break

if __name__ == "__main__":
    roll_dice()

没有休息声明。

import random

def roll_dice():
    total = 0
    flag = True
    while flag:
        print ("Enter '1' to roll 2 dice, enter '2' to stop rolling")
        user_input = input()
        if user_input == 1:
            # Get die_one and die_two entry
            die_one, die_two  = random.randint(1,6), random.randint(1,6)
            print ("Got %d in die_one and %d in die_two" % (die_one, die_two))
            score = 0
            if die_one == die_two:
                score = 10
            elif die_one == 6 or die_two ==6: 
                score = 4
            elif die_one + die_two == 7:
                score = 7

            print ("The current score is %d" % score)
            total += score
            print ("The total score is %d " % total)

        elif user_input == 2:
            print ("Thanks for playing!")
            flag = False

if __name__ == "__main__":
    roll_dice()

答案 2 :(得分:0)

使用itertools重构以创建骰子并提升评分逻辑。

import random
import itertools as it
import collections as ct


def roll(n=2):
    """Return a random result of two die."""
    return random.choice(list(it.product(range(1,7), repeat=n)))


def score(die):
    """Return a score for a given rule."""
    if len(set(die)) == 1:
        return 10
    elif 6 in die:
        return 4
    elif sum(die) == 7:
        return 7
    else:
        return 0


def dice_game():
    """Run game."""
    total = 0
    while True:
        user_input = input("Enter '1' to roll two die.  Enter '2' to stop rolling. ")
        if user_input == "1":
            result = roll()
            pts = score(result)
            total += pts

            print("Result:", result)
            print ("The score this round is", pts)
            print ("The total number of points is", total)
            print()

        elif user_input == "2":
            print ("Thanks for playing!")
            return

dice_game()