Pythonにおけるif条件文

1537 ワード

if–else文
  • 各if条件の後にコロン(:)を使用し、次に条件を満たす後に実行する文
  • を表す.
  • Pythonにswitch-case文がない
  • ticket = 1
    if ticket == 1:  #        (==),        (=)
        print("   ,         ")
        print("      ")
    else:
        print("   ,       ")
        print("       ,   ")
    
  • if-elif文
  • #if-elif    (        ,if-else       )
    score = 60
    if score > 90:
        print("         ")
    elif  90>score>80:
        print("      ,    ")
    elif  80>score>70:
        print("      ")
    elif score<70:
        print("    cai ")
    

    ifネスト文
  • 多重判定ではifネスト文
  • を用いることができる.
    #if  , if  if-else     if  if-else  
    #      :
    ticket = 1 # 1    , 0    
    knife_length = 9  #      9cm
    if ticket == 1:
        print("   ,    ")
        if knife_length <=10:
            print("    ,     ")
        else:
            print("      ")
            print("       10cm,    ,      ")
    else:
        print("    ,        ")
    
    

    if-elif-else文の使用
    #    
    import random #random()              ,  [0,1)   
    player_imput =input("   (0  、1  、2 ):")
    player =int(player_imput)
    computer = random.randint(0,2)
    if (player ==0 and computer == 2) or (player ==1 and computer == 0)\
        or (player == 2 and computer == 1):
        print("   ,   ")
    elif (player ==2 and computer == 0) or (player ==0 and computer == 1)\
        or (player == 1 and computer == 2):
        print("    ,     ")
    else:
        print("  ,   ")