python基礎小程式

來源:互聯網
上載者:User

標籤:python基礎小程式

========================================

猜大小的遊戲

#!/usr/bin/python

# -*- coding: UTF-8 -*-


import random

s = int(random.uniform(1,100))

#print(s)

m = 1

i = 0  

while m != s:

m = int(input('輸入你猜的整數:'))

i += 1

    if m > s:

        print('你猜的數字大了,不要灰心,繼續努力')

    if m < s:

        print('你猜的數字小了,不要灰心,繼續加油')

    if m == s:

if i <= 4:

print('這麼快就猜對了,太神了')

elif i == 5:

print('5次就猜對了,果然厲害')

else :

print i,"次總算猜對了,恭喜恭喜"

        break;

===============================


===============================

猜拳小遊戲


#!/usr/bin/python

# -*- coding: UTF-8 -*-

import random

while 1:

    s = int(random.randint(1, 3))

    if s == 1:

        ind = "石頭"

    elif s == 2:

        ind = "剪子"

    elif s == 3:

        ind = "布"

    m = raw_input('輸入 石頭、剪子、布,輸入"end"結束遊戲:')

    blist = ['石頭', "剪子", "布"]

    if (m not in blist) and (m != 'end'):

        print "輸入錯誤,請重新輸入!"

    elif (m not in blist) and (m == 'end'):

        print "\n遊戲退出中..."

        break

    elif m == ind :

        print "電腦出了: " + ind + ",平局!"

    elif (m == '石頭' and ind =='剪子') or (m == '剪子' and ind =='布') or (m == '布' and ind =='石頭'):

        print "電腦出了: " + ind +",你贏了!"

    elif (m == '石頭' and ind =='布') or (m == '剪子' and ind =='石頭') or (m == '布' and ind =='剪子'):

        print "電腦出了: " + ind +",你輸了!"


===============================


===============================

搖篩子遊戲


#!/usr/bin/env python3

# -*- coding: utf-8 -*-

import random

import sys

import time


result = []

while True:

    result.append(int(random.uniform(1,7)))

    result.append(int(random.uniform(1,7)))

    result.append(int(random.uniform(1,7)))

    print result

    count = 0

    index = 2

    pointStr = ""

    while index >= 0:

        currPoint = result[index]

        count += currPoint

        index -= 1

        pointStr += " "

        pointStr += str(currPoint)

    if count <= 11:

        sys.stdout.write(pointStr + " -> " + "小" + "\n")

        time.sleep( 1 )   # 睡眠一秒

break

    else:

        sys.stdout.write(pointStr + " -> " + "大" + "\n")

        time.sleep( 1 )   # 睡眠一秒

break

    result = []


===============================


===============================

 九九乘法表


#!/usr/bin/python

# -*- coding: UTF-8 -*-

#九九乘法表


i = 1

while i :

    j = 1

    while j:

        print j ,"*", i ," = " , i * j , '  ',

        if i == j :

            break


        j += 1

        if j >= 10:

            break

    

    print "\n"

    i += 1

    if i >= 10:

        break

===============================


===============================

去除字串首尾的空格:


def trim(s):

    while s[:1] == ' ':

        s = s[1:]

    while s[-1:] == ' ':

        s = s[:-1]

    return s


str = '   Runoob     '

print(trim(str))

===============================


===============================

十進位轉二進位


#!/usr/bin/python

# -*- coding: UTF-8 -*-


denum = input("輸入十進位數:")

print denum,"(10)",

binnum = []

# 位元

while denum > 0:

    binnum.append(str(denum % 2)) # 棧壓入

    denum //= 2

print '= ',

while len(binnum)>0:

    import sys

    sys.stdout.write(binnum.pop()) # 無空格輸出print ' (2)'

==============================


==============================

判斷質數


#!/usr/bin/python

# -*- coding: UTF-8 -*-

for num in range(2,100):  # 迭代 10 到 20 之間的數字

   for i in range(2,num): # 根據因子迭代

      if num%i == 0:      # 確定第一個因子

         j=num/i          # 計算第二個因子

#         print '%d 等於 %d * %d' % (num,i,j)

         break            # 跳出當前迴圈

   else:                  # 迴圈的 else 部分

      print num, '是一個質數'

==============================

=============================

# 列印空心等邊三角形

#!/usr/bin/python

# -*- coding: UTF-8 -*-

rows = int(raw_input('輸入行數:'))

for i in range(0, rows):

    for k in range(0, 2 * rows - 1):

        if (i != rows - 1) and (k == rows - i - 1 or k == rows + i - 1):

            print " * ",

        elif i == rows - 1:

            if k % 2 == 0:

                print " * ",

            else:

                print "   ",

        else:

            print "   ",

    print "\n"

==============================


冒泡排序

#!/usr/bin/python

# -*- coding: UTF-8 -*-


arays = [1,5,2,7,6,15,12,9]

for i in range(len(arays)):

    for j in range(i+1):

        if arays[i] < arays[j]:

            # 實現連個變數的互換

            arays[i],arays[j] = arays[j],arays[i]

print arays

==========================


==========================

列印菱形


#!/usr/bin/python

# -*- coding: UTF-8 -*-


width = int(5)

for row in range(width + 1):

    for col in range(width + 1):

        if ((abs(row - width/2) + abs(col - width/2)) == width/2):

            print "*",

        else:

            print " ",

    print " "


====================================




python基礎小程式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.