Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
硬币翻转程序,打印正面或反面的百分比/python_Python - Fatal编程技术网

硬币翻转程序,打印正面或反面的百分比/python

硬币翻转程序,打印正面或反面的百分比/python,python,Python,所以我对编码是新手,我正在尝试写一个掷硬币的程序。它必须掷硬币1000次,给出人头的百分比,然后我再掷50000次,再得到人头的百分比。这就是我现在所拥有的一切,我不知道该从这里走到哪里 def flipCoin(): heads=0 for i in range(1000): coin=random.randint(1,2) if random.randint==1: print ("Heads") heads+=1 percent=

所以我对编码是新手,我正在尝试写一个掷硬币的程序。它必须掷硬币1000次,给出人头的百分比,然后我再掷50000次,再得到人头的百分比。这就是我现在所拥有的一切,我不知道该从这里走到哪里

def flipCoin():
  heads=0
  for i in range(1000):
    coin=random.randint(1,2)
    if random.randint==1:
      print ("Heads")
      heads+=1
      percent=(heads/10)*100
      print (percent)
    else:
      print

以下是您试图编写的代码的要点:

flip a coin
see if it's heads, add to a global counter
divide global counter by total number of tries (this is the percentage)

你所要做的就是尝试1000次和50000次。从您已经发布的代码片段中,您已经获得了翻转和计数功能—现在只需确定百分比,并在1000次和50000次尝试中运行它即可。:)如果您有任何问题,请告诉我。

一个好方法是在函数中添加一个参数

例如,您可以使用参数
num\u of\u flips

def flipCoin(num_of_flips):
    # function body
然后,无论何时调用该函数。您可以将值作为参数传递。像这样:

flipCoin(100) # flip coin 100 times
flipCoin(5000) # flip coin 5000 times
以下是我的完整flipCoin()函数:

def flipCoin(num_of_flips):
    heads = 0
    for i in range(num_of_flips):
        coin=random.randint(1, 2)
        if coin==1:
            heads += 1

    percent = heads / num_of_flips
    print(percent)

flipCoin(100) # flip coin 100 times
flipCoin(5000) # flip coin 5000 times

此外,你需要正确计算百分比。百分比应等于头数除以总翻转数。这就是我在上面代码中所做的。

大家好,欢迎来到stackoverflow

第一件事:既然你需要掷硬币n次,为什么不在你的函数中反映出来,比如

def flipCoin(times):
    # ...
    for i in range(times):
下一步:要做出正面或反面的决定,我们只需要一个选项是“Falsy”(意味着隐式bool cast的计算结果为False),另一个选项不是
random.randint(0,1)
给出了这一点,因此我们可以使用这个随机整数作为头部的条件:

if random.randint(0,1):
    heads += 1
只需打印结果:

print('heads percentage: %05.2f' % (100 * float(heads) / times) )

我已经改进了你的代码。我的python知识也不是很好,但这里是:

import random

def flipCoin():
    heads = 0 # track heads amount
    tails = 0 # track tails amount
    headspercent = 0 # heads percentage
    tailspercent = 0 # tails percentage

    for i in range(1000): # run the experiment 1000 times
      coin=random.randint(1,2) # assign a value to coin, either 1 or 2

      if coin==1: # if coin value is assigned as 1
          heads+=1 # increase heads count by 1
      else: # if coin value is assigned something other than 1
          tails+=1 # increase tails count by 1

    headspercent = heads / 10.0 # since we're rolling 1000 times, /10 will give percentage
    tailspercent = 100.0 - headspercent # no need to recalculate 100 - heads = tails %

    print("Heads percent: " + str(headspercent)) # printing the values on screen
    print("Tails percent: " + str(tailspercent)) # converting numbers to string by str()



flipCoin() # calling the function
下面是代码的输出:

Python 3.6.1 (default, Dec 2015, 13:05:11)
[GCC 4.8.2] on linux

Heads percent: 50.6
Tails percent: 49.4
我已经解释了代码中哪一行做什么。我希望这个答案能帮助你更好地理解这个话题


编辑:我已经尽力使它尽可能简单,有更高级的方法来解决这个问题。

您的代码似乎不完整。把所有代码都发出来,你的思路是对的。想想你应该用什么来划分
头。现在你把它除以10,但那是不对的。这是我正在写的一个更大的程序的一部分,但这就是我为这一部分所做的一切。如果你只需要打印磁头的总量,我会将打印语句移出for循环。这会占用很多时间,而且会让它运行得很慢。此外,您正在将
random.randint
1
进行比较,而不是将
coin
进行比较。。这通常意味着你需要的是半个小时的时间和当地的导师在一起,或是走一走,而不是堆积如山。在这种情况下,放慢速度--一次识别并纠正一个问题。