如何根据输出

时间:2016-01-27 20:25:44

标签: python python-3.x

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次”?

感谢。解释也很好,以便我可以学习

3 个答案:

答案 0 :(得分: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'

答案 1 :(得分:1)

创建一个名为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)
...

答案 2 :(得分:0)

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

使用字典进行比较将允许您运行for循环来比较它,比使用6个elif语句更有效,使代码更有效。

最后,当用户键入" n"为了退出并检查他的卷,我们将again.lower()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() 进行比较,以防万一用户键入" N"代替。

然后它循环通过字典并打印' time / roll'或者' times / rolls'基于简单的if语句。

我希望这对你有意义,如果不只是问!

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

此代码在运行几次时输出以下内容:

private void SubmitButton_Click(object sender, RoutedEventArgs e) {

  // Try to parse the numbers
  double  numberOne, numberTwo;
  bool isTextOneNumber = Double.TryParse(text1.Text, out numberOne);
  bool isTextTwoNumber = Double.TryParse(text2.Text, out numberTwo);
  if(isTextOneNumber && isTextTwoNumber )
  {
     //calculate what you want with numberOne, numberTwo
  }
  else
  {
    //provide some error(validation) message
  }
}