Python编程例子:修订间差异

来自牛奶河Wiki
跳到导航 跳到搜索
无编辑摘要
无编辑摘要
第46行: 第46行:
   choice(lis)</code>  
   choice(lis)</code>  


=== 4.发扑克牌 ===
<code> values = list(range(1,11)) + 'Jack Queen King'.split()  #列表,list(range...
  suits = 'diamond clubs hearts spades'.split()          #[diamond,clubs,hearts,spades]
  deck = ['%s of %s'%(v, s) for v in values for s in suits]  #两个for,最终是个列表
 
<code> from pprint import pprint  #自动换行
  pprint(deck[:12])          #只选12张(按顺序)显示如下</code>
['1 of diamond',
'1 of clubs',
'1 of hearts',
'1 of spades',
'2 of diamond',
'2 of clubs',
'2 of hearts',
'2 of spades',
'3 of diamond',
'3 of clubs',
'3 of hearts',
'3 of spades']
<code> from random import shuffle #
  shuffle(deck)              #序列元素随机排序
  pprint(deck[:12])          #选12张(最终随机)显示如下 </code>
['5 of hearts',
'1 of hearts',
'1 of diamond',
'6 of clubs',
'Jack of spades',
'2 of diamond',
'King of spades',
'8 of diamond',
'Queen of clubs',
'6 of diamond',
'9 of clubs',
'9 of spades']


[[分类:Python]]
[[分类:Python]]

2024年1月12日 (五) 11:42的版本

1.编写 斐波那契数列 的初始子序列

a, b = 0,1
   while a < 10:
   print(a)
   a, b= b, a+b

或者

def fab(max): 
   n, a, b = 0, 0, 1 
   while n < max: 
       print b 
       a, b = b, a + b 
       n = n + 1
fab(5)

2.今天吃什么

 #今天吃什么.py
   import fileinput,random
   lunch = list(fileinput.input(openhook=fileinput.hook_encoded('utf-8',)))
   print(random.choice(lunch))

菜单.txt

汉堡王

庆丰包子

魏家凉皮

霸蛮米粉

你的心跳酸菜鱼

食堂

山西刀削面

KFC

吉野家

调用: python 今天吃什么.py 菜单.txt

3.今天吃什么水果-简版

 from random import *
  lis = ['苹果','梨','樱桃','车厘子','菠萝','波罗蜜','榴莲','橘子','橙子','柚子']
  choice(lis) 

4.发扑克牌

 values = list(range(1,11)) + 'Jack Queen King'.split()  #列表,list(range...
 suits = 'diamond clubs hearts spades'.split()           #[diamond,clubs,hearts,spades]
 deck = ['%s of %s'%(v, s) for v in values for s in suits]  #两个for,最终是个列表
 
 from pprint import pprint  #自动换行
 pprint(deck[:12])          #只选12张(按顺序)显示如下 

['1 of diamond',

'1 of clubs',

'1 of hearts',

'1 of spades',

'2 of diamond',

'2 of clubs',

'2 of hearts',

'2 of spades',

'3 of diamond',

'3 of clubs',

'3 of hearts',

'3 of spades']

 from random import shuffle #
 shuffle(deck)              #序列元素随机排序
 pprint(deck[:12])          #选12张(最终随机)显示如下  

['5 of hearts',

'1 of hearts',

'1 of diamond',

'6 of clubs',

'Jack of spades',

'2 of diamond',

'King of spades',

'8 of diamond',

'Queen of clubs',

'6 of diamond',

'9 of clubs',

'9 of spades']