条件循环字符串

#完成完整的温度转换程序
while True:
    a = int(input('摄氏转华氏请按1\n华氏转摄氏请按2:\n'))

    if a == 1:
        #用户输入摄氏度
        celsius = float(input('输出摄氏温度:'))
        #计算华氏温度
        fahrenheit = (celsius * 1.8)+32 #f = c*9/5+32
        #向用户输出华氏摄氏度
        print('{:.2f}摄氏温度转化为华氏温度为{:.2f}'.format(celsius,fahrenheit))#
    elif a == 2:
        fahrenheit = float(input('请输入华氏温度:'))
        celsius = 5 / 9 * (fahrenheit - 32)
        print('{:.2f}华氏温度转化为摄氏温度为:{:.2f}\n'.format(fahrenheit,celsius))
    else:
        break

 

 

 

#猜数字游戏
import random
s = random.randint(1,10)
b = int(input('请随机输入数字:'))
while s !=b:
    if s>b:
        print('亲,数字太小了,请重新输入')
        b = int(input('请随机输入数字:'))
    if s<b:
        print('亲,数字太大了,请重新输入')
        b = int(input('请随机输入数字:'))
else:
    print('亲,您猜对了^-^')

 



 

 

 

#解析身份证不同片段的含义
myid = '440105199610184667'
if myid[:4] == '4401':
area = "广东省广州市"
if int(myid[-2])%2 == 0:
sex = '女'
else:
sex = '男'
age = 2018 -int(myid[6:10])
print("你是"+area+"的居民,"+"性别:"+sex+",年龄:"+str(age))

#解析学号的含义
x='201606050061'
a=x[0:4]
print('年级:'+a)
b=x[4:9]
print('专业:'+b)
c=x[8:12]
print('班级:'+c)

 

 

 

#用for循环产生一系列网址
for i in range(2,10):


print('http://news.gzcc.cn/html/2018/xiaoyuanxinwen/{}.html'.format(i))



字符串的连接
str1='沉醉于'
str2='风中'
print(str1+str2)

#字符串的重复
str = 'Hello World!'
print (str * 2)
str1 = '我很快乐\n'*3
print('str1:{0}'.format(str1))


#字符的in判断
s="u can u up,no can no bb"
res = "bb" in s
print(res)






#用for循环遍历字符串
s="iamsorry"
for c in s:
    print("字母:"+c)



 

 



posted @ 2018-09-13 22:57  zhongwolin  阅读(284)  评论(0编辑  收藏  举报