Skip to main content

365 DAYS OF CODE - DAY 4

DAY 4 THOUGHT PROCESS

We learned randomization, modules, generating random integers and floating point numbers (always less than 1 but positive) using the random.random() function.

For the code to print heads or tails exercise, I only saw head once although I was not really concentrating on the screen. I tried running till I got another but gave up eventually.

My code:

import random

random_number = random.randint(
1, 10)
if random_number/2 == 1:
   
print("Heads")
else:
   
print("Tails")

Correct code:

import random

random_number = random.randint(
1, 10)
if random_number%2 == 1:
   
print("Heads")
else:
   
print("Tails")

We learned Lists data structure; a way of organizing and storing data, the index out of range error (i.e., IndexError), nested lists, did a lot of struggling and asked a friend for help.

Final code:

import random

rock =
'''
    _______
---'   ____)
      (_____)
      (_____)
      (____)
---.__(___)
'''

paper = '''
    _______
---'   ____)____
          ______)
          _______)
         _______)
---.__________)
'''

scissors = '''
    _______
---'   ____)____
          ______)
       __________)
      (____)
---.__(___)
'''

possible_choice = [rock, paper, scissors]
computer_choice = random.choice(possible_choice)
user_choice =
input("rock, paper, or scissors: ")

if user_choice == "rock":
    user_choice = rock
elif user_choice == "paper":
    user_choice = paper
elif user_choice == "scissors":
    user_choice = scissors

if computer_choice == user_choice:
   
print(f"computer chose {computer_choice}, you chose {user_choice}")
   
print("It's a tie!")
elif computer_choice == rock and user_choice == paper:
   
print(f"computer chose {computer_choice}, you chose {user_choice}")
   
print("You win!")
elif computer_choice == rock and user_choice == scissors:
   
print(f"computer chose {computer_choice}, you chose {user_choice}")
   
print("You lose!")
elif computer_choice == paper and user_choice == rock:
   
print(f"computer chose {computer_choice}, you chose {user_choice}")
   
print("You lose!")
elif computer_choice == paper and user_choice == scissors:
   
print(f"computer chose {computer_choice}, you chose {user_choice}")
   
print("You win!")
elif computer_choice == scissors and user_choice == paper:
   
print(f"computer chose {computer_choice}, you chose {user_choice}")
   
print("You lose!")
elif computer_choice == scissors and user_choice == rock:
   
print(f"computer chose {computer_choice}, you chose {user_choice}")
   
print("You win!")
else:
   
print("Invalid input. Please try again")

 

Testing up while you write the code is very good practice. 

Plus, you gotta write your own code and you gotta make it work.

:)

Comments

Popular posts from this blog

365 DAYS OF CODE - DAY 0

DAY 0 THOUGHT PROCESS Day Zero of the 365 days of code highlights my state of mind before the actual start of the program. Yes, it is a program. A program I set for myself to make sure I actually put in the work on a daily. I have tried coding before but have never been quite consistent with it. A friend advised me to try coding daily if I really wanted to be great at it someday. He also stated that it would take a long time and I should be prepared for it. During the same period, I noticed he was on a year's worth streak of chess which made me realize I could also do the same but with coding. I signed up for the 100 days of code course on Udemy and plan on starting my coding journey with Angela Yu and the thousands of students also taking the course. I also drafted my own copy of the pledge, copied and modified from the 100 Days of Python Pledge from the course materials. It is currently pasted on my wall as a constant reminder. Finally, I will also be recording my process here...

365 DAYS OF CODE - DAY 13

What is wrong with this code?? Can you tell what it does?? import art n1 = 0 n2 = 0 selected_operation = "" solution = 0 def add (n1, n2): return n1 + n2 def subtract (n1, n2): return n1 - n2 def multiply (n1, n2): return n1 * n2 def divide (n1, n2): return n1 / n2 operations = { "+" : add, "-" : subtract, "*" : multiply, "/" : divide } def rest_of_calculation (): print ( "+ \n - \n * \n /" ) selected_operation = input ( "Pick an operation: " ) n2 = float ( input ( "What is the next number?: " )) for key in operations: if key == selected_operation: solution = operations[key](n1, n2) print ( f" { n1 } { key } { n2 } = { solution } " ) print (art.logo) n1 = float ( input ( "What is the first number?: " )) rest_of_calculation() while True : new_calculation = input ( f"Type 'y' to continue calcu...

365 DAYS OF CODE - DAY 27

  Watched a friend write these: from menu import Menu, MenuItem from coffee_maker import CoffeeMaker from money_machine import MoneyMachine def what_to_do_next (): pass def handle_prompt (prompt): if prompt == "off" : coffee_on = False return elif prompt == "report" : report = coffee_maker.report() print (report) coffee_maker = CoffeeMaker() coffee_on = True while coffee_on == True : prompt = input ( "What would you like? (espresso/latte/cappuccino): " ) handle_prompt(prompt)