python2.7练习小例子(二十一)

    21):1、题目:两个乒乓球队进行比赛,各出三人。甲队为a,b,c三人,乙队为x,y,z三人。已抽签决定比赛名单。有人向队员打听比赛的名单。a说他不和x比,c说他不和x,z比,请编程序找出三队赛手的名单。

    程序源代码:

 

#!/usr/bin/python
# -*- coding: UTF-8 -*-

for i in range(ord('x'),ord('z') + 1):
    for j in range(ord('x'),ord('z') + 1):
        if i != j:
            for k in range(ord('x'),ord('z') + 1):
                if (i != k) and (j != k):
                    if (i != ord('x')) and (k != ord('x')) and (k != ord('z')):
                        print 'order is a -- %s\t b -- %s\tc--%s' % (chr(i),chr(j),chr(k))

    以上实例输出结果为:

 

order is a -- z     b -- x    c--y

    

#!/usr/bin/python
# -*- coding: UTF-8 -*-

for a in ['x','y','z']:
    for b in ['x', 'y', 'z']:
        for c in ['x', 'y', 'z']:
            if(a!=b)and(b!=c)and(c!=a) and (a!='x') and (c!='x') and (c!='z'):
                print 'a和%s比赛,b和%s比赛,c和%s比赛' %(a,b,c)

    

#!/usr/bin/python
# -*- coding: UTF-8 -*-

n=['a','b','c']
m=[]
for i in range(3):
    if n[i]!='a' and n[i]!='c':
        m.insert(i,'x')
    elif n[i]!='c':
        m.insert(i,'z')
    else:
        m.insert(i,'y')

print 'a--%s, b--%s, c--%s' %(m[0], m[1], m[2])

    

#coding:UTF-8
n=['a','b','c']
m=['x','y','z']
L=[]
for i in range(0,3):
    for j in range(0,3):
        L.append(n[i]+m[j])
L.remove('ax')
L.remove('ay')
L.remove('by')
L.remove('bz')
L.remove('cx')
L.remove('cz')
print(L)

    Python2.x 与 Python3.x 均可执行:

 

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import itertools

A = ["a", "b", "c"]
B = ["x", "y", "z"]
team = []  # 存储比赛名单
rankB = [list(each) for each in itertools.permutations(B)]  # 将对手的全部组合方式列出来
while True:
    flag = 0
    team = list(zip(A, B))  # 匹配选手
    print(team)
    for each in team:
        if (("a" in each) and ("x" in each)) or (("c" in each) and (("x" in each) or ("z" in each))):  # 判断是否符合对阵要求
            flag = 1  # 如不符合则打个标记
            break
    if flag:
        B = rankB.pop()  # 改变一下对手位置
    else:
        break

for v1, v2 in team:
    print("%s 对阵 %s" % (v1, v2))

   2、题目:打印出如下图案(菱形):

 

   *
  ***
 *****
*******
 *****
  ***
   *

    程序分析:先把图形分成两部分来看待,前四行一个规律,后三行一个规律,利用双重for循环,第一层控制行,第二层控制列。

    程序源代码:

 

#!/usr/bin/python
# -*- coding: UTF-8 -*-

from sys import stdout
for i in range(4):
    for j in range(2 - i + 1):
        stdout.write(' ')
    for k in range(2 * i + 1):
        stdout.write('*')
    print
 
for i in range(3):
    for j in range(i + 1):
        stdout.write(' ')
    for k in range(4 - 2 * i + 1):
        stdout.write('*')
    print

    以上实例输出结果为:

 

   *
  ***
 *****
*******
 *****
  ***
   *

    Python3 环境还可以使用以下方式实现:

 

#!/usr/bin/python3

for i in range(1, 5):
    print(' ' * (4 - i), end="")
    for j in range(1, 2 * i):
        print('*', end="")
    print()
for i in range(3, 0, -1):
    print(' ' * (4 - i), end="")
    for j in range(1, 2 * i):
        print('*', end="")
    print()

    Python3 环境还可以使用以下方式实现:

 

#!/usr/bin/python3

def pic(lines):    
    middle, lines = int(lines / 2), int(lines / 2) * 2 + 1    
    for i in range(1, lines + 1):        
        empty = abs(i - middle - 1)        
        print(' ' * empty, '*' * (2 * (middle - empty) + 1))
line = 7 # 设置输出行数
pic(7)

    Python3 环境还可以使用以下方式实现:

 

#!/usr/bin/python3

n = int(input('enter a number:')) 
for  i  in range(1,n+1,2):
    k = (n-i)//2
    print( ' '* k , '*' * i)

for  p in range(n-2,0,-2):
    o = (n-p)//2
    print(' '*o, '*'*p)

    

#!/usr/bin/env python
# -*- coding: utf-8 -*-

a = '*'
xx = []
for i in range(1, 5):
    x = ''
    for j in range(2*i-1):
        x += a
    xx.append(x)
    print x.center(7, ' ')
    if x == "*******":
        xx.reverse()
        for k in range(0, len(xx)):
            print xx[k].center(7, ' ')

    

#usr/bin/env python3
#coding:utf-8

for i in range(4):
    print((3-i)*' '+(2*i+1)*'*')
for i in range(3):
    print((i+1)*' '+(5-2*i)*'*')

    

#!/usr/bin/python
# -*- coding: UTF-8 -*-

def funs(x):
    i = 0
    n = 1
    k = x
    while i<x:
        print  k * ' ', n*'*',k * ' '
        i = i+1
        n = n+2
        k = k-1
    n= n-2
    k=1
    while i*2>x-1:
        k=k+1
        i=i-1
        n=n-2
        print  k * ' ', n * '*', k * ' '
funs(4)

    Python3 环境还可以使用以下方式实现:

 

#!/usr/bin/python3

def printsome(n):
    for index in range(abs(4 - n)):
        print(" ", end="")
    for index in range((4- abs(4 - n))*2 -1):
        print("*", end="")
    print()
for index in range(1, 8):
    printsome(index)

    可以设定行数:

 

#!/usr/bin/python
# -*- coding: UTF-8 -*-

n=int(raw_input("请输入行数:"))
if n%2==0:
    n1=n/2
    n2=n1-1
    for i in range(1,n1+1):
        m=i*2
        print ' '*n2,'*'*m
        n2-=1
    n2=1
    for i in range(n1-1):
        m-=2
        print ' '*n2,'*'*m
        n2+=1
else:
    n1=n//2+1
    n2=n1-1
    for i in range(n1):
        m=i*2+1
        print ' '*n2,'*'*m
        n2-=1
    n2=1
    for i in range(n1-1):
        m-=2
        print ' '*n2,'*'*m
        n2+=1

    

#usr/bin/env python
#coding:utf-8

n = 4
for i in range(n):
    a=' '*(n-1-i)+'*'*(i*2+1)
    print(a)
for j in range(n-1):
    a=' '*(1+j)+'*'*(2*n-j*2-3)
    print(a)

    

#!/usr/bin/env python
# -*- coding: utf-8 -*-

n = int(input("请输入行数 n:"))
for i in range(0,n):
    a = abs(i - int(n/2))
    b = n - abs(i - int(n/2))
    print(" "*a+"*"*(b-a))

    

#!/usr/bin/python
# -*- coding: UTF-8 -*- 

def print_diamond(n):
    star = ['*']
    for index in range(0,n):
        print '{:^7}'.format(star[index])
        star1 =star[index] + '**'
        star.append(star1)
    for i in range(n-2,-1,-1):
        print '{:^7}'.format(star[i])
if __name__ == '__main__':
    print_diamond(4)

    

for x in range(1,8)[::2]:print ("*"*x).center(10)
for x in range(1,6)[::-2]:print ('*'*x).center(10)

    可以自由输入菱形的腰长,自定义大小,如果不输入值,直接回车即是默认的如题目示例那样的图形:

 

# encoding:utf-8

def paintlx(l):
    for i in range(l / 2):
        print ' ' * (l / 2 - i) + '*' * (i * 2 + 1)
    print '*' * l
    for i in range(l / 2 - 1, -1, -1):
        print ' ' * (l / 2 - i) + '*' * (i * 2 + 1)
while True:
    l = raw_input('请输入菱形腰长(奇数),默认为7:')
    if l == '':
        l = 7
        paintlx(int(l))
        break
    elif int(l) % 2 == 0:
        print '说了是奇数!'
    else:
        paintlx(int(l))
        break

    参考实例,可用于 Python2.x 与 Python3.x:

 

# encoding:utf-8

for i,j in zip(range(3,-1,-1),range(1,9,2)):
    print(" "*i+"*"*j)
for k,t in zip(range(1,4),range(5,0,-2)):
    print(" "*k+"*"*t)

    Python3 测试实例:

 

for i in range(4):
    print(' '*(3-i),'*'*(2*i+1),' '*(3-i))

for i in list(range(3))[::-1]:
    print(' '*(3-i),'*'*(2*i+1),' '*(3-i))

    上述两个实例都是比较经典的也是比较有用的。寻找了各种各样的实例,大家参考下。如果感觉不错的话,请多多点赞支持哦。。。

  原文链接:https://blog.csdn.net/luyaran/article/details/80063337

posted @ 2018-04-25 13:12  luyaran  阅读(461)  评论(0编辑  收藏  举报