【Python Practice】Day 17 Question 65-69

'''
@Author: your name
@Date: 2020-07-23 23:13:36
@LastEditTime: 2020-07-23 23:35:12
@LastEditors: Please set LastEditors
@Description: In User Settings Edit
@FilePath: \vscode_py\day17.py
'''
# Question 65
# Please write assert statements to verify that every number in the list [2,4,6,8] is even.

def Q65():
    l=[2,4,6,8]
    for i in l:
        assert i%2==0,"{} is not even".format(i)
    print("end")

# Question 66
# Please write a program which accepts basic mathematic expression from console and print the evaluation result.

def Q66():
    expression=input("expression: ")
    ans=eval(expression)
    print(ans)


# Question 67
# Please write a binary search function which searches an item in a sorted list. The function should return the index of element to be searched in the list.

def Q67(li,element):
    import math
    low=0
    high=len(li)-1
    # print(high)

    while low<=high:
        mid=round((high+low)/2)
        # print(mid)
        if element>li[mid]:
            low=mid+1
        elif element<li[mid]:
            high=mid-1
        else:
            return mid
    return None

# 使用random.uniform(a,b)生成指定范围(a,b)的随机数
# Question 68
# Please generate a random float where the value is between 10 and 100 using Python module.
def Q68():
    import random
    print("random:{}".format(random.uniform(10,100)))

# Question 69
# Please generate a random float where the value is between 5 and 95 using Python module.
def Q69():
    import random
    # ans=random.random()*100-5
    ans=random.uniform(5,95)
    print("ans:{}".format(ans))



if __name__ == "__main__":
    # Q65()

    # Q66()

    # li=[2,4,6,8]
    # print(Q67(li,5))

    # Q68()

    Q69()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Tech沉思录

点赞加投币,感谢您的资瓷~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值