zoukankan      html  css  js  c++  java
  • day12

    day12
    1. 练习题第八题,将数字的每一位都+2,转换成一个新数字(整数和小数)
    def number(s):
    “”“如果不是int或者float类型就提示输入数字,就剔除了字母的情况,包括多个.等异常情况”“”
    new_s = []
    if isinstance(s,float): # 如果是float类型,用.分割两边的数,然后再根据长度相加2
    ss = str(s).split('.')
    s1 = ss[0]
    s2 = ss[1]
    s1_len = len(s1)*'2'
    s2_len = len(s2)*'2'
    new_s1 = int(s1)+int(s1_len)
    new_s2 = int(s2)+int(s2_len)
    new_s = "{}.{}".format(new_s1,new_s2)
    print(new_s)
    elif isinstance(s, int):
    ss = len(str(s))
    add_num = ss*'2'
    new_num = str(int(s) + int(add_num))
    print(new_num)
    else:
    print("please input number!")
    控制流
    2.设定一个用户名和密码,用户输入正确的用户名和
    密码,则显示登录成功,否则提示登录失败,用户最多失败
    3 次,否则退出程序
    user = {'name':'zhangsan','password':'123456'}
    def login(user)
    for i in range(3):
    name = str(input("please input name:"))
    password = str(input('please input password:'))
    if name == user['name'] and password == user['password']:
    print("login success")
    break
    else:
    print('login error')
     
    3.自己实现一个函数,在一句话中查找某个单词的算
    法,存在返回索引号,否则返回 False
    def find_index(s,index):
    if isinstance(s,str) and isinstance(index,str):
    if index in s:
    index_len = len(index)
    for i in range(len(s)):
    if s[i:i+index_len] == index:
    return [i,i+index_len]
    else:
    return False
    else:
    print('please input str')
     
    4.随机生成一个整数,1-100 之间你最多猜 5 次,如
    果猜大了,提示大了小了,提示小了,猜对了,提示猜中。
    5 次都没猜中,就猜没猜中。
    import random
    target = random.randint(1,100)
    print("当前随机数字:%s" % target)
    for i in range(5):
    num = int(input('请输入你猜的数字:'))
    if num > target:
    print('猜大了')
    elif num < target:
    print("你猜小了")
    elif num == target:
    print("你猜对了")
    print("你猜了%s次" %str(i+1))
    break
    else:
    print("异常错误")
    if i == 4:
    print('你的机会用完了')
     
    5.关于格式化输出%s 和 %d
    >>> i = 1
    >>> print("%d" % i)
    1
    >>> print("%d" % i+1)
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    TypeError: can only concatenate str (not "int") to str
    >>> print("%d" %i+1)
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    TypeError: can only concatenate str (not "int") to str
    >>> print("%d" %(i+1))
    2
    >>> print("%s" %str(i+1))
    2
     

  • 相关阅读:
    rotate list
    使用存取方法来设置Property value
    模拟创建类变量,static变量加类方法,单例
    下拉刷新常规代码
    强制横竖屏间切换
    友盟分享
    快速下拉刷新动画
    把电脑上的视频导入苹果6
    xcrun: error: active developer path ("/Volumes/Xcode/Xcode-beta.app/Contents/Developer") does not exist, use `xcode-select --swi
    iOS 刚刚,几分钟前,几小时前,几天前,几月前,几年前
  • 原文地址:https://www.cnblogs.com/jueshilaozhongyi/p/12082196.html
Copyright © 2011-2022 走看看