python基礎:條件循環字符串

1、html

完成完整的溫度轉換程序python

while True:

    a = int(input('攝氏度轉換爲華氏溫度請按1\n華氏溫度轉化爲攝氏溫度請按2\n'))

    if a == 1:

        celsius = float(input('輸入攝氏溫度:'))

        fahreaheit = (celsius + 1.8) + 32  # f = c+9/5+32

        print('{:.2f}攝氏溫度轉爲華氏溫度爲{:.2f}'.format(celsius, fahreaheit))

    elif a == 2:

        celsius1 = float(input('輸入華氏溫度:'))

        fahreaheit1 = (celsius1 - 32) * 5 / 9

        print('{:.2f}華氏溫度轉化爲攝氏溫度爲{:.2f}'.format(celsius1, fahreaheit1))

    else:

        break;

 運行結果:dom

 

2、ui

猜數字遊戲(猜價格,猜年齡等)編碼

import random

secret = random.randint(1,10)

#print(secret)

print('猜數字遊戲')

guess = -1

while guess != secret:

    a = input('請輸入數字:')

    guess = int(a)

    if guess > secret:

        print('輸入的數字太大!')

    elif guess < secret :

        print('輸入的數字過小!')

    else :

        print('恭喜你,猜對了!')

 運行結果:code

 

3、orm

解析身份證號、學號不一樣片斷的含義htm

id='500104111111191689'

a="省份編碼:"+id[0:2]
b="地區編碼:"+id[2:4]
c="縣區編碼:"+id[4:6]
d="出生年月日;"+id[6:10]+"年"+id[10:12]+"月"+id[12:14]+"日"
e="戶口所在派出所編碼:"+id[14:16]
f="性別編碼:"+id[16:17]
h="校驗碼:"+id[17:18]

print('該同窗身份信息是:\n'+a,b,c,d,e,f,h)

stuid="201606050063"
print("年級"+stuid[0:4])
print("專業"+stuid[4:8])
print("班級"+stuid[8:10])
print("序號"+stuid[10:])

  

運行結果:blog

4、遊戲

字符串的:鏈接,重複,in判斷

 

s='hello,'
t='我是XXX'
#字符串鏈接
print(s+t)
#字符串重複
print(s*2)
print('o' in s)

  運行結果:

5、

用for產生一系列網址:

for i in range(0,30,2):  #查詢0-30條校園新聞中的偶數網址
    print('http://news.gzcc.cn/html/xiaoyuanxinwen/' + str(i) + '.html')   #方法一
    print('http://news.gzcc.cn/html/xiaoyuanxinwen/{}.html'.format(i))    #方法二

  運行結果:

 

6、

用for循環產生字符串遍歷:

str = 'Hello'
for i in str:
print(i)

運行結果:

相關文章
相關標籤/搜索