• Python之if-else语句


    if--else语句
    if username == 'admin' and password == '123456': print('身份验证成功!') else: print('身份验证失败!')
    if--elif--else语句
    if 判断语句:
      执行语句
    elif 判断语句:
      执行语句
    else:
      执行语句
    记住if后千万不要加(),这与c语言不一样。

    #1题

    import math

    for i in range(0,3):
        a,b,c=map(float,input('Enter a,b,c: ').split())
        def panbie(x,y,z):#判别函数,返回float类型 P
            p=y*y-4*x*z
            return p
        def suanR1(x,y,z):#计算第一个根的函数,返回float类型 R1
            r1=(-y+math.sqrt(y*y-4*x*z))/2*x
            return r1
        def suanR2(x,y,z):#计算第二个根的函数,返回float类型 R2
            r2=(-y-math.sqrt(y*y-4*x*z))/2*x
            return r2
        if panbie(a,b,c)>0:
            print('%.4f %.4f'% (suanR1(a,b,c),suanR2(a,b,c)))
        elif panbie(a,b,c)==0:
            print(suanR1(a,b,c))
        else:
            print('The equation has no real roots!')
    #2题
    import random
    def sum(x,y,z):
        if z==x+y:
            return True
        else:
            return False
    a=random.randrange(0,100)
    b=random.randrange(0,100)
    print(a,b,sep=",")
    c=int(input('请输入两个数的和:'))
    print(sum(a,b,c))
    #3题
    def further(a,b):
        c=(a+b)%7
        if c==0:
            return print('星期日')
        elif c==1:
            return print('星期一')
        elif c==2:
            return print('星期二')
        elif c==3:
            return print('星期三')
        elif c==4:
            return print('星期四')
        elif c==5:
            return print('星期五')
        elif c==6:
            return print('星期六')
    day=int(input('输入今天: '))
    lastday=int(input('输入将要过几天: '))
    further(day,lastday)
    
  • 相关阅读:
    leetcode-62-Unique Paths
    [leetcode-64-Minimum Path Sum]
    [leetcode-198-House Robber]
    [leetcode-120-Triangle]
    [leetcode-53-Maximum Subarray]
    [leetcode-303-Range Sum Query
    [leetcode-123-Best Time to Buy and Sell Stock III]
    绑定下拉框
    解决URL参数中文乱码
    html 图片拖动不出来的脚本
  • 原文地址:https://www.cnblogs.com/shy13138/p/11286540.html
Copyright © 2020-2023  润新知