Python Bootcamp 100 Days of Code

Honda.putha

Well-known member
  • Dec 26, 2017
    17,242
    29,818
    113
    Python:
    import random
    from replit import clear
    from art import logo
    
    def deal_card():
      cards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
      return random.choice(cards)
    
    def calculate_score(cards):
      if sum(cards) == 21 and len(cards) == 2:
        return 0
     
      if 11 in cards and sum(cards) > 21:
        cards.remove(11)
        cards.append(1)
     
      return sum(cards)
    
    def compare(user_score,computer_score):
    
      if user_score == computer_score:
        print("Drawn!")
      elif computer_score == 0:
        print("Computer wins with a BlackJack")
      elif user_score == 0:
        print("User win with  a BlackJack")
      elif user_score > 21:
        print("User loses with over scoring.")
      elif computer_score > 21:
        print("Computer lose with over scoring. ")
      elif computer_score > user_score:
        print("Computer win.")
      elif computer_score < user_score:
        print("User win.")
     
    
    def play_game():
    
      print(logo)
    
      is_game_over = False
    
      user_cards = []
      computer_cards = []
    
      for _ in range(2):
        user_cards.append(deal_card())
        computer_cards.append(deal_card())
    
    
      while not is_game_over:
    
        user_score = calculate_score(user_cards)
        computer_score = calculate_score(computer_cards)
        print(f"   Your cards: {user_cards}, current score: {user_score}")
        print(f"   Computer's first card: {computer_cards[0]}")
        
        if user_score == 0 or computer_score == 0 or user_score >21:
          is_game_over = True
        else:
          continue_deal = input("Press 'y' if you want to deal another card or 'n' to stop: ").lower()
          if continue_deal == 'y':
            user_cards.append(deal_card())
          else:
            is_game_over = True
        
      if not (user_score == 0 or computer_score == 0 or user_score >21):
        while computer_score < 17:
          computer_cards.append(deal_card())
          computer_score = calculate_score(computer_cards)
    
      print(f"   Your final hand: {user_cards}, final score: {user_score}")
      print(f"   Computer's final hand: {computer_cards}, final score: {computer_score}")
      print(compare(user_score, computer_score))
    
    while input("Do you want to continue playing BlackJack? 'y' ").lower() == 'y':
      clear()
      play_game()

    මේ කෝඩ් එකෙන් None කියලා ප්‍රින්ට් වෙනවානෙ. කොහෙන්ද හොයාගන්න බෑ.

    1625790327746.png


    #Day 12 done
    ------ Post added on Jul 9, 2021 at 8:13 AM
     
    • Like
    Reactions: MihiCherub

    Honda.putha

    Well-known member
  • Dec 26, 2017
    17,242
    29,818
    113
    • Love
    Reactions: MihiCherub

    MihiCherub

    Well-known member
  • Sep 14, 2009
    18,849
    1
    9,594
    113
    Gampaha
    • Like
    Reactions: Honda.putha

    MihiCherub

    Well-known member
  • Sep 14, 2009
    18,849
    1
    9,594
    113
    Gampaha
    #Day_24 - Intermediate
    Files, Directories and Paths (1hr 3min)

    Completed...

    #Day_23 - Intermediate
    The Turtle Crossing Capstone Project (38min)

    Skip karapu lesson ekath ada kala. habai tikai kale. fast forward dala kale.
     
    Last edited:
    • Love
    Reactions: Honda.putha

    Honda.putha

    Well-known member
  • Dec 26, 2017
    17,242
    29,818
    113
    mama omi game eka finish kale na. uba deck eka haduwe kohomada?
    Got two lists as Values and Suits.
    Then add a mix of those to a list of lists.
    So a single card is a list and it is a member of deck_of_card list.

    [['8', 'Diamonds'], ['7', 'Spades'], ['6', 'Clubs'], ['J', 'Hearts'], ['7', 'Hearts'], ['K', 'Diamonds'], ['A', 'Hearts'], ['6', 'Hearts'], ['9', 'Spades'], ['9', 'Diamonds'], ['6', 'Diamonds'], ['K', 'Hearts'], ['9', 'Hearts'], ['Q', 'Clubs'], ['A', 'Clubs'], ['8', 'Hearts'], ['A', 'Diamonds'], ['7', 'Diamonds'], ['Q', 'Diamonds'], ['K', 'Spades'], ['9', 'Clubs'], ['K', 'Clubs'], ['8', 'Spades'], ['8', 'Clubs'], ['J', 'Clubs'], ['Q', 'Spades'], ['A', 'Spades'], ['7', 'Clubs'], ['J', 'Spades'], ['Q', 'Hearts'], ['6', 'Spades'], ['J', 'Diamonds']]
     
    • Like
    Reactions: MihiCherub

    MihiCherub

    Well-known member
  • Sep 14, 2009
    18,849
    1
    9,594
    113
    Gampaha
    Got two lists as Values and Suits.
    Then add a mix of those to a list of lists.
    So a single card is a list and it is a member of deck_of_card list.

    [['8', 'Diamonds'], ['7', 'Spades'], ['6', 'Clubs'], ['J', 'Hearts'], ['7', 'Hearts'], ['K', 'Diamonds'], ['A', 'Hearts'], ['6', 'Hearts'], ['9', 'Spades'], ['9', 'Diamonds'], ['6', 'Diamonds'], ['K', 'Hearts'], ['9', 'Hearts'], ['Q', 'Clubs'], ['A', 'Clubs'], ['8', 'Hearts'], ['A', 'Diamonds'], ['7', 'Diamonds'], ['Q', 'Diamonds'], ['K', 'Spades'], ['9', 'Clubs'], ['K', 'Clubs'], ['8', 'Spades'], ['8', 'Clubs'], ['J', 'Clubs'], ['Q', 'Spades'], ['A', 'Spades'], ['7', 'Clubs'], ['J', 'Spades'], ['Q', 'Hearts'], ['6', 'Spades'], ['J', 'Diamonds']]
    elaz man haduwe mehema..
    Python:
    suits  = ['hearts', 'diamonds', 'spades', 'clubs']
    deck = [(suit,value) for suit in suits for value in range(2,15)]
     

    Honda.putha

    Well-known member
  • Dec 26, 2017
    17,242
    29,818
    113
    #Day 15 - Intermediate is finished.

    I have a problem.

    Why doesn't she use the dictionaries directory from the calling method (e.g. make_coffee())?

    She passes the references to the dictionaries as arguments.

    elaz man haduwe mehema..
    Python:
    suits  = ['hearts', 'diamonds', 'spades', 'clubs']
    deck = [(suit,value) for suit in suits for value in range(2,15)]
    Python:
    values = ['6','7','8','9','J','Q','K','A']
    suits = ['Spades','Hearts','Diamonds','Clubs']
    
    def deck_of_cards():
      deck_of_cards = []
      for suit in suits:
        for value in values:
            card = [value,suit]
            deck_of_cards.append(card)
    
      random.shuffle(deck_of_cards)
      return deck_of_cards

    I did it similar way.
    ------ Post added on Jul 12, 2021 at 7:46 AM
     
    • Like
    Reactions: MihiCherub

    Honda.putha

    Well-known member
  • Dec 26, 2017
    17,242
    29,818
    113
    me eka widihak

    Python:
    resources = {
        "water": 300,
        "milk": 200,
        "coffee": 100,
    }
    
    
    def add_all_together():
        sum = 0
        for key in resources:
            sum = sum + resources[key]
        return sum
    
    
    is_on = True
    
    while is_on:
        add_all_together()
        is_on = False

    Python:
    resources = {
        "water": 300,
        "milk": 200,
        "coffee": 100,
    }
    
    
    def add_all_together(available_resources):
        sum = 0
        for key in available_resources:
            sum = sum + available_resources[key]
        return sum
    
    
    is_on = True
    
    while is_on:
        add_all_together(resources)
        is_on = False

    me anik widiha.

    පලවෙනි එකේදි resources කියන dictionary එක පාස් කරන්නෙ නෑ argument ekak විදිහට,def add_all_together():.

    එහෙමයි මම ලිව්වෙ.

    ඒත් Dr. ලියනකොට ඒවා arguments විදිහට පාස් කරලා දෙවෙනියට දාලා තියෙන විදිහට def add_all_together(available_resources):.

    එක නිකන්ම access කරන්න පුලුවන් නම් එහෙම කරන්නෙ ඇයි?
     

    MihiCherub

    Well-known member
  • Sep 14, 2009
    18,849
    1
    9,594
    113
    Gampaha
    ඊලගට මෙන්න මේ දේ දැනගන්න ඕනෙ.me eka widihak

    Python:
    resources = {
        "water": 300,
        "milk": 200,
        "coffee": 100,
    }
    
    
    def add_all_together():
        sum = 0
        for key in resources:
            sum = sum + resources[key]
        return sum
    
    
    is_on = True
    
    while is_on:
        add_all_together()
        is_on = False

    Python:
    resources = {
        "water": 300,
        "milk": 200,
        "coffee": 100,
    }
    
    
    def add_all_together(available_resources):
        sum = 0
        for key in available_resources:
            sum = sum + available_resources[key]
        return sum
    
    
    is_on = True
    
    while is_on:
        add_all_together(resources)
        is_on = False

    me anik widiha.

    පලවෙනි එකේදි resources කියන dictionary එක පාස් කරන්නෙ නෑ argument ekak විදිහට,def add_all_together():.

    එහෙමයි මම ලිව්වෙ.

    ඒත් Dr. ලියනකොට ඒවා arguments විදිහට පාස් කරලා දෙවෙනියට දාලා තියෙන විදිහට def add_all_together(available_resources):.

    එක නිකන්ම access කරන්න පුලුවන් නම් එහෙම කරන්නෙ ඇයි?
    මේක ටිකක් ලොකු මාතෘකාවක් මචන්. scope lesson එක පොඩ්ඩක් බලන්නකො. ඒතකොට ටිකක් තේරුම් ගන්න පුලුවන්.

    Mutable and Immutable Data Types in Python​

    • Some of the mutable data types in Python are list, dictionary, set and user-defined classes.
    • On the other hand, some of the immutable data types are int, float, decimal, bool, string, tuple, and range.

    ඊලගට මෙන්න මේ දේ දැනගන්න ඕනෙ.

    pass by value and pass by reference​

    https://www.geeksforgeeks.org/pass-by-reference-vs-value-in-python/

    එතකොට ඔතන වැරද්ද තියෙන්නෙ Good practice vs Bad practice.
     
    Last edited:

    Honda.putha

    Well-known member
  • Dec 26, 2017
    17,242
    29,818
    113
    මේක ටිකක් ලොකු මාතෘකාවක් මචන්. scope lesson එක පොඩ්ඩක් බලන්නකො. ඒතකොට ටිකක් තේරුම් ගන්න පුලුවන්.



    ඊලගට මෙන්න මේ දේ දැනගන්න ඕනෙ.

    pass by value and pass by reference​

    https://www.geeksforgeeks.org/pass-by-reference-vs-value-in-python/

    එතකොට ඔතන වැරද්ද තියෙන්නෙ Good practice vs Bad practice.
    Thank you. Now understood!

    #Day 16 done

    But have an issue. It doesn't decrease the available resources.

    Python:
     if user_choice == 'report':
            coffee_maker.report()
            money_machine.report()
        elif user_choice == 'off':
            is_on = False
        else:
            drink = menu.find_drink(user_choice)
            if coffee_maker.is_resource_sufficient(drink):
                if money_machine.make_payment(drink.cost):
                    coffee_maker.make_coffee(drink)

    Above code.

    And here the output.

    bug.PNG


    Note that report doesn't change.
    ------ Post added on Jul 14, 2021 at 1:11 AM

    # Day 17 done.
    ------ Post added on Jul 14, 2021 at 12:38 PM
     
    • Like
    Reactions: MihiCherub

    MihiCherub

    Well-known member
  • Sep 14, 2009
    18,849
    1
    9,594
    113
    Gampaha
    Thank you. Now understood!

    #Day 16 done

    But have an issue. It doesn't decrease the available resources.

    Python:
     if user_choice == 'report':
            coffee_maker.report()
            money_machine.report()
        elif user_choice == 'off':
            is_on = False
        else:
            drink = menu.find_drink(user_choice)
            if coffee_maker.is_resource_sufficient(drink):
                if money_machine.make_payment(drink.cost):
                    coffee_maker.make_coffee(drink)

    Above code.

    And here the output.

    View attachment 135114

    Note that report doesn't change.
    ------ Post added on Jul 14, 2021 at 1:11 AM

    # Day 17 done.
    ------ Post added on Jul 14, 2021 at 12:38 PM
    if coffee_maker.is_resource_sufficient(drink):
    me method eka danna.




    #Day_25 - Intermediate
    Working with CSV Data and the Pandas Library (1hr 14min)

    header row එක skip කරන්න if else conditions ලියනවට වඩා මෙහෙම කරන්න පුලුවන්.
    use islice
    Python:
    import csv
    from itertools import islice
    
    with open("weather_data.csv") as file:
       data = csv.reader(file)
    
       for row in islice(data, 1, None):
          print(row[1])

    එහෙම නැත්නම් next use කරල පුලුවන්
    Python:
    import csv
    
    with open("weather_data.csv") as file:
       data = csv.reader(file)
       next(data, None)
    
       for row in data:
          print(row[1])

    Turtle මගුල නම් දැන් ඕනවට වැඩී හොදටම. අන්තිම ටික කලේ නෑ

    Pandas library cheat sheet
    https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=&cad=rja&uact=8&ved=2ahUKEwibrPb7lt7xAhV0yzgGHSaJB-MQFjAAegQIBRAD&url=https://pandas.pydata.org/Pandas_Cheat_Sheet.pdf&usg=AOvVaw2Z0H-ttrFe-41ta-Cnkf55
     
    • Love
    Reactions: Honda.putha