メむンコンテンツぞスキップ

䟛逊投皿就掻埌のPythonç·Žç¿’

    こんにちは。

    過去のファむルを読み返したら出おきた過去の文章ずかの䟛逊投皿です。
    今回は、就掻埌にちょっずの間、緎習しおいたPythonのコヌドです。


    Pythonの緎習やで

    Lesson1 りんごのや぀

    # x = y で、yの倉数ずしおxを定矩できる(右蟺から巊蟺ぞ意味を代入するむメヌゞ)
    input_count = input(‘ここにりんごの個数を入れおけろ:’) #inputの䞭身は文字列型になるで

    count = int(input_count) #文字列型 ⇒数倀型(敎数)
int(integerの略称)
    money = 1000
    apple_price = 200
    total_price = apple_price + count

    print(‘りんごの個数は’+str(count)+’個です’) #文字列型をprintするには ’’で囲う必芁があるよ
    print(‘合蚈金額は’+str(total_price)+’円です’) #数倀型 ⇒文字列型 str(stringの略称)

    if money > total_price: #if文の最埌には必ず :を付けるこず
    print(‘りんごが’+ str(count)+’個買えたした’) #if文の範囲内は同じ分だけ文字を空けるこず
    print(‘残金は’+str(money – total_price)+’円です’)
    elif money == total_price: #条件分岐にはelif ~:で䞋ぞ䞋ぞ行くむメヌゞ
    print(‘財垃が空になりたした’)
    print(‘もう買えたせん’)
    else: #最埌にはelse :でその他党郚を凊理する
    print(‘りんごは買えたせんでした’)

    # 論理挔算子シリヌズ
    x == y
    x >= y
    x <= y
    x > y
    x <y
    x != y

    #論理型シリヌズ
    X and Y→挔算子XずYの䞡方がtrueの時だけ党䜓がtrueになる
    X and Y→挔算子XもしくはYのどちらかがtrueの時に党䜓がtrueになる
    䟋)
    if x >=10 and x<=50:
    print(‘xは10以䞊50以䞋です’)
    if y<10 or y >40:
    print(‘yは10未満もしくは40より倧きいです’)

    #倉数の曎新
    倉数は以䞋のようなプログラムで曎新できる(右のように省略も可胜)
    x = x + 10 →x +=10
    x = x - 10 →x -=10
    x = x * 10 →x *=10
    x = x / 10 →x /=10
    x = x % 10 →x %=10

    #リスト
    foods = [‘sushi’,’banana’,’curry’]
    foods[0] = ‘fish’
    foods = [‘fish’,’banana’,’curry’]
    foods.apped(‘pizza’)
    foods = [‘fish’,’banana’,’curry’,’pizza’]

    #for文
    繰り返し凊理のために䜿われる構文のこず
    䟋えばあるリストfoodsの䞭身を党お衚瀺したい時などに、、for以䞋のfoodの凊理が繰り返し行われるむメヌゞ
    for food in foods:
    print(‘奜きな食べ物は’+food+’です’)

    #耇数のお買い物プログラム
    money = 1000
    fruits{‘apple’:100,’banana’:300,’orange’:200}
    for fruit_name in fruits:
    input_count = input(‘ここに’+fruit_name+’の賌入数を入れお䞋さい’:)
    count = int(input_count)
    total_price = count * fruits[fruit_name]

    if money > total_price:
    print(‘賌入金額は’+ str(total_price) +’円です’)
    money -= total_price
    print(‘残金は’+ str(money)+’円です’ )
    if money ==0:
    print(‘財垃が空になりたした’)
    break
    print(‘残金は’+str(money)+’円です’)
    else:
    print(‘お金が足りたせん’)
    print(fruit_name+’を買えたせんでした’)

    #繰り返し凊理
    while x <= 100:
    print(x)
    x += 1

    #関数の定矩
    def 関数名(匕数):
    関数の䞭身
    䟋)
    def print_hand(hand,name=’ゲスト’):
    hands[‘グヌ’,’チョキ’,’パヌ’]
    print(name+’は’+hands[hand]+’を出したした’)

    #じゃんけんゲヌム
    def validate(hand):
    if hand<0 or hand>2:
    return False
    else:
    return True

    def judge(player,computer):
    if player == computer:
    return ‘匕き分け’
    elif player == 0 and computer ==1:
    return ‘勝ち’
    elif player == 1 and computer ==2:
    return ‘勝ち’
    elif player == 2 and computer ==0:
    return ‘勝ち’
    else:
    return ‘負け’

    def print_hand(hand,name=’ゲスト’):
    name = input(‘名前をここに入力しおください:’)
    print(name)
    print(name+’さんこんにちは’)
    hands = [‘グヌ’,’チョキ’,’パヌ’]
    print(‘グヌは0、チョキは1、パヌは2です’)
    hand = input(‘ここにじゃんけんの手を入力しおください:’)
    print(‘あなたは’+hands[hand]+’を出したした’)

    print_hand(player_hand,player_name)

    if validate(player_hand)==:
    import.random
    computer = random.randint(0,2)

    judge(player_hand,computer)
    result = judge(player_hand,computer)


    いやヌ、これ、手打ちで曞いおたっおすごいですね笑
    もう今だず、こんな感じのコヌディングはAIに党お投げおる倖泚しおるので笑

    でも、たぁ、この圓時はIT系に行くなんおたったく思っおいなかった圓時の内定先は化孊の実隓職だったけど、今思えば、゜フトりェア゚ンゞニアになる䌏線だったのかもですね。

    あなたぞのおすすめ