用python做一个程序:扔100次硬币,然后分别显示出掷出正面和反面的次数 关于硬币的python问题

作者&投稿:赵赖 (若有异议请与网页底部的电邮联系)

7行代码即可写出程序,详细步骤:

1、首先打开python自带的IDLE,打开IDLE并ctrl+n新建如图界面。

2、导入random模块的choice功能。

3、在列表中放置up和down这两个元素,这里表示正面朝上和正面朝下。因为不考虑硬币立着的奇葩设定,所以只加这个上和下。并且把列表赋予给coin这个变量。

4、接下来使用for循环来遍历,range后面为抛硬币次数,这里我想程序运行一次只抛硬币一次,所以range后面设置为1。

5、这一行,就是if判断了,如果随机从列表中抽取到了up。

6、那么就要输出“正面”。

7、否则,就输出“反面”。

8、至此此代码完成,按F5即可运行,运行一次就抛一次。



import random
count = 0 # 1 正面 0 反面 记录1的次数
for i in range(100):
    n = random.randint(0,1)
    if n == 1:
        count += 1
print(u'正面{0}次,反面{1}次'.format(count,100-count))



import random

num = 0
fcount = 0
bcount = 0
while True:
    n = random.randint(0,1)
    num +=1
    if num>100:
        break
    
    if n == 1:
        fcount += 1
  
    else:
        bcount += 1
 
print("正面:",fcount)

print("反面:",bcount)
print("投掷次数",fcount+bcount)


方法一:
import random

head=0
for i in range(100):
n = random.randint(0,1)
if n==1:
head+=1

print('正面{}次,反面{}次'.format(head,100-head))

方法二
import random

head=0
tail=0
for i in range(100):
n = random.randint(0,1)
if n==1:
head+=1
else:
tail+=1
print('正面',head,'反面',tail)

import random
a=[random.randint(0,1) for i in range(100)]
print(a.count(0))
print(a.count(1))


编写一个Python程序, 模拟抛硬币一百万次,显示出现正面和反面的次数.~

import random
count = 0 # 1 正面 0 反面 记录1的次数
for i in range(10000000):
n = random.randint(0,1)
if n == 1:
count += 1
print(u'正面{0}次,反面{1}次'.format(count,1000000-count))

# coding:utf8import randomdef chkcoin(acoin):basecoin = [1, 5, 10, 25]flag = Falsefor bc in basecoin:if acoin == int(bc):return Trueelse:flag = Trueif flag:print 'Invalid entry'return Falsedef tryAgain():comd = raw_input('Try again (y/n)?: ')if comd == 'y':return Trueelif comd == 'n':print 'Thanks for playing ... goodbye'return Falseelse:print 'Command error! Please enter y or n.'print 'Thanks for playing ... goodbye'return Falseif __name__ == '__main__':print'''The purpose of this exercise is to enter a number of coin valuesthat add up to a displayed target value.Enter coins values as 1-penny, 5-nickel, 10-dime, and 25-quarter.Hit return after the lase entered coin value.-----------------------''',rand = random.randint(1, 99)print 'Enter coins that add up to %s cents, one per line.' % randisAgain = Truewhile isAgain:coins = []acoin = int(raw_input('Enter first coin: '))if chkcoin(acoin):coins.append(acoin)else:continuewhile True:tolcoin = 0for coin in coins:tolcoin += int(coin)print tolcoinif tolcoin == rand:print 'Correct!'if not tryAgain():isAgain = Falsebreakelif tolcoin > rand:print 'Sorry - total amount exceeds %s cents.' % randif not tryAgain():isAgain = Falsebreakelse:passacoin = int(raw_input('Enter next coin: '))if chkcoin(acoin):coins.append(acoin)else:continueif isAgain:rand = random.randint(1, 99)print 'Enter coins that add up to %s cents, one per line.' % rand 运行结果:
Python 2.7.8 (default, Jun 30 2014, 16:03:49) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>
The purpose of this exercise is to enter a number of coin values
that add up to a displayed target value.

Enter coins values as 1-penny, 5-nickel, 10-dime, and 25-quarter.
Hit return after the lase entered coin value.
-----------------------
Enter coins that add up to 1 cents, one per line.
Enter first coin: 1
1
Correct!
Try again (y/n)?: y
Enter coins that add up to 72 cents, one per line.
Enter first coin: 25
25
Enter next coin: 25
50
Enter next coin: 25
75
Sorry - total amount exceeds 72 cents.
Try again (y/n)?: n
Thanks for playing ... goodbye
>>>

我这里用的是list来存储输入的数字,累加,这只是一种方法,你可以加工精简。。。也可以用楼上兄弟的方法逐减,然后和0比较。希望你能解决这个问题。

python 如何将一系列数字十个一行输出
答:results = list(range(10, 90))n = 10 # 每10个数换一行 for i in range(len(results)):print(results[i], end=' ')if (i+1) % 10 == 0:print("\n") # \n为转义符 换行的意思#out:拓展内容python list列表 序列是Python中最基本的数据结构。序列中的每个元素都分配一个...

帮用python做个小程序
答:我也是刚学python,下面是我的程序,试了下,大致没问题。def print_title(seq):seq_len = len(seq)screen_width = 80 box_width = seq_len + 6 left_margin = (screen_width - box_width) // 2 print()print(' '*left_margin + '+' + '-'*(box_width-2) + '+')print(' '*...

用python写一个小程序,让用户输入任意9个数字,然后输出排序后的结果...
答:input a number please: "))#控制输入转为int\x0d\x0a data.append(num)#放入列表\x0d\x0a data.sort()#排序\x0d\x0a print data#输出\x0d\x0a\x0d\x0apython的程序扩展名是.py\x0d\x0a程序执行结果:\x0d\x0a>>> littleFunc()\x0d\x0ainput a number please: 1...

随机产生一个3位整数,将他的十位数变为0的python怎么做?
答:最初被设计用于编写自动化脚本(shell),随着版本的不断更新和语言新功能的添加,越多被用于独立的、大型项目的开发。Python的创始人为荷兰人吉多·范罗苏姆(Guido van Rossum)。1989年圣诞节期间,在阿姆斯特丹,Guido为了打发圣诞节的无趣,决心开发一个新的脚本解释程序,作为ABC 语言的一种继承。

用python写一个猜数字程序。不要用函数。运行结果如图。
答:import randomwhile 1: mx = int( input( "请输入猜数范围(50以内):1-" ) ) while (mx > 50 or mx < 1): mx = int( input( "请输入猜数范围(50以内):1-" ) ) print( "下面将产生一个1-{0}的随机数".format( mx ) ) num = random.Random().randint( ...

Python程序题?
答:b. map函数的作用;--- 先说列表推导,最简单的列表推导其实就是for循环的简写形式,举例来说:上面这三句,如果用列表推导的形式写:[num for num in range(10)]上面二者是等价的, 可以看出列表推导的形式,相比常规形式,省略了空列表的声明语句以及元素的追加语句 .还有一个比较常见的形式是,for和if...

用python写一个程序?
答:第四盘胜利赢得比赛,则有三种可能 第五盘获得胜利,有六种可能 总共结果是10盘,再换成B赢得比赛,结果是10 总共20 第六盘获得胜利,则胜利方最后一盘赢得比赛,这表明另一方在前面已经赢得三盘,表示A赢的前提是B赢,与事实矛盾,说明bo6赢三盘的情况并不存在,前提应该是bo5 程序如下:程序 如...

Python编写程序,输入3个不重复的1位正整数,输出所有能用这3个1位正...
答:将三个数字组成一个序列 seq = [a, b, c]使用 permutations 函数获取所有长度为 3 的排列组合 perms = itertools.permutations(seq, 3)遍历所有排列组合,将每个组合转化为整数并输出 for perm in perms:num = perm[0] * 100 + perm[1] * 10 + perm[2]print(num)该程序会首先让用户输入...

用python来做一个程序
答:self, tag, attrs):if tag == 'a':for name,value in attrs:if name == 'href':print value print self.get_starttag_text()创建HTML解析器的实例 lParser = parseLinks()打开HTML文件 lParser.feed(urllib.urlopen( \ "http://www.python.org/index.html").read())lParser.close()

python做一个计算器小程序要求当输入任意数字每输入一个数字相加当输入...
答:好的,以下是一个简单的python计算器小程序,可以满足您的需求:初始化总和为0 total = 0 while True:获取用户输入 num = int(input("请输入数字(输入0结束计算):"))如果输入的是0,则退出循环 if num == 0:break 否则将输入的数字累加到总和中 total += num 输出计算结果 print("计算结果...