Python基础:条件循环字符串

(1)完成完整温度转换

while True:
    a = int(input('摄氏度转换为华氏温度请按1
华氏温度转化为摄氏温度请按2
'))

    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;

  运行结果:

(2)猜数字游戏

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('猜对了!')

运行结果:

(3)解析身份证号码

s = '511024199805050024'
a = s[:2]
b = s[2:4]
c = s[4:6]
d = s[6:14]
e = s[14:16]
f = s[-2]
g = s[-1]
print('省份{}'.format(a))
print('地区{}'. format(b))
print('县级{}'. format(c))
print('出生日期{}'.format(d))
print('顺序码{}'.format(e))

if int(s[-2]) % 2 == 0:
    print('性别:女')
else:
    print('性别:男')
    print('校验码{}'.format(g))

运行结果:

(4)学号制作

s ='201606050056'
a=s[:4]
b=s[4:6]
c=s[6:8]
d=s[8:]
print('年级{}'.format(a))
print('学院{}'.format(b))
print('班级{}'.format(c))
print('学号{}'.format(d))

运行结果:

(5)for 循环 语句 产生网站

for i in range(2,10,2):
    print('http://news.gzcc.cn/html/xiaoyuanxinwen/'+ str(i) + '.html')
    print('http://news.gzcc.cn/html/xiaoyuanxinwen/''.html'.format(i))

运行结果:

原文地址:https://www.cnblogs.com/moon2/p/9639524.html