python翻转
Here, we are going to build a coin() function using python. The program is so simple as an introductory program for defining a function. The function is going to use an inbuilt library naming random. This random library helps us to choose a random value of the variable within the range.
在这里,我们将使用python构建coin()函数 。 该程序非常简单,就像定义功能的入门程序一样。 该函数将使用命名为random的内置库。 这个随机库可帮助我们选择该范围内变量的随机值。
random.choice(['H','T'])
The above function will choose a random value with a probability of 0.5 each i.e. all are independent to each other which will work as dice.
上面的函数将选择一个随机值,每个概率为0.5,即所有值彼此独立,将它们作为骰子工作。
Here is the code
这是代码
import random
# function to return the random value of
# coin flip
def coin():
return random.choice(['H','T'])
# main code i.e. calling the function
print('COIN FLIP: ', coin())
print('COIN FLIP: ', coin())
print('COIN FLIP: ', coin())
print('COIN FLIP: ', coin())
print('COIN FLIP: ', coin())
Output
输出量
COIN FLIP: H
COIN FLIP: T
COIN FLIP: T
COIN FLIP: H
COIN FLIP: T
翻译自: https://www.includehelp.com/python/program-to-design-a-coin-flip-function.aspx
python翻转