繁体   English   中英

如何根据输出将“时间”替换为“时间”

[英]How to replace “times” with “time” depending on the output

import random
total = [0]
one = 0
two = 0
three = 0
four = 0
five = 0
six = 0

dice=True
while dice:
a = random.randrange(1,7)
if a == 1:
    one = one + 1
elif a == 2:
    two = two + 1
elif a == 3:
    three = three + 1
elif a == 4:
    four = four + 1
elif a == 5:
    five = five + 1
elif a == 6:
    six = six + 1


b = len(total)
print ("Roll:", b,)
print ("The dice has rolled:",a,)
total.append (a)




dice =input("Roll again? (y,n):")
if dice == "n":
    print ("Thank-You!")
    print ("One rolled",one,"times")
    print ("Two rolled",two,"times")
    print ("Three rolled",three,"times")
    print ("Four rolled",four,"times")
    print ("Five rolled",five,"times")
    print ("Six rolled",six,"times")

    break

我如何才能做到,如果“一个”仅被“一次”滚动,它会显示“一个已滚动时间”,而不是“一个已滚动1次”?

谢谢。 一个解释也很好,这样我就可以学习

您可以使用str.format并检查数字是否已精确滚动一次。 演示:

>>> one = 1
>>> 'One rolled {} time{}'.format(one, 's' if one!=1 else '')
'One rolled 1 time'
>>> one = 0
>>> 'One rolled {} time{}'.format(one, 's' if one!=1 else '')
'One rolled 0 times'
>>> one = 3
>>> 'One rolled {} time{}'.format(one, 's' if one!=1 else '')
'One rolled 3 times'

创建一个名为printTimesRolled的函数或类似的函数。 然后传递一个字符串和一个int。 像这样:

def printTimesRolled(numberWord, timesRolled):
    if (timesRolled == 1):
        print(numberWord, "rolled 1 time.")
    else:
        print(numberWord, "rolled", timesRolled, "times.")

然后,要打印所有内容,只需执行以下操作:

printTimesRolled("One", one)
printTimesRolled("Two", two)
...

这将是处理骰子滚动的好方法。 我不确定您是否已经开始探索类,但是通过制作DiceRoller类,您可以使它更加井井有条。 此外,避免大量的全局变量。

使用字典进行比较将使您可以运行一个for循环来进行比较,而不是拥有6条elif语句,从而使代码更高效。

最后,当用户键入“ n”退出并检查其滚动时,如果用户键入“ N”,我们将再次与again.lower()进行故障保护。

然后,它循环遍历字典并基于简单的if语句打印“时间/滚动”或“时间/滚动”。

我希望这对您有意义,即使不只是问!

import random

# DEFINING THE CLASS
class DiceRoller(object):
    def __init__(self):
        self.sides = {1:0,
                      2:0,
                      3:0,
                      4:0,
                      5:0,
                      6:0}

    # WILL ROLL THE DICE AND ADD 1 TO THE SIDE COUNTER WHEN A SIDE IS ROLLED
    def rollTheDice(self):
        roll = random.randint(1,6)

        for side in (self.sides):
            if roll == side:
                self.sides[side] += 1

        print('Rolled a %s' % (roll))

    # WILL PRINT TOTAL ROLLS WHEN CALLED
    def getTotals(self):
        for i, side in enumerate(self.sides):
            if self.sides[side] == 1:
                print('Side %s: %s roll' % (i + 1, self.sides[side]))
            else:
                print('Side %s: %s rolls' % (i + 1, self.sides[side]))


# THIS CODE IS EXECUTED
Dice = DiceRoller()

while True:
    Dice.rollTheDice()
    again = input('Roll again [y/n]: ')

    if again.lower() == 'n':
        break

Dice.getTotals()

多次运行时,此代码输出以下内容:

Rolled a 2
Roll again [y/n]: y
Rolled a 4
Roll again [y/n]: y
Rolled a 3
Roll again [y/n]: y
Rolled a 1
Roll again [y/n]: y
Rolled a 4
Roll again [y/n]: y
Rolled a 2
Roll again [y/n]: n
Side 1: 1 roll
Side 2: 2 rolls
Side 3: 1 roll
Side 4: 2 rolls
Side 5: 0 rolls
Side 6: 0 rolls

Process finished with exit code 0

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM