Skip to main content

365 DAYS OF CODE - DAY 6

THOUGHT PROCESS

Today we covered defining and calling functions, Reeborg’s World, indentation (i.e., tabs or 4 spaces), I might watch Silicon Valley, I prefer tabs over spaces because you just have to do it once.

We also covered the while loop (and compared it to the for loop), the infinite loop (print out your condition if you do not know why you are getting an infinite loop.),

Reeborg Hurdle 1 Loop Challenge:


My Code:

def turn_right():

    turn_left()

    turn_left()

    turn_left()

   

def jump():

    turn_left()

    move()

    turn_right()

    move()

    turn_right()

    move()

    turn_left()

   

def move_and_jump():

    move()

    jump()

 

for reeborg in range(6):

    move_and_jump()

 

# I am actually getting 1% better each day now

 

Reeborg Hurdle 2:


My Code:

def turn_right():

    turn_left()

    turn_left()

    turn_left()

   

def jump():

    turn_left()

    move()

    turn_right()

    move()

    turn_right()

    move()

    turn_left()

   

def move_and_jump():

    move()

    jump()

 

while True:

    move_and_jump()

    if at_goal() == True:

        break

I did not use negation, ouch.

 

Reeborg Hurdle 3:


My Code:

# What you need to know

# The functions move() and turn_left().

# The conditions front_is_clear() or wall_in_front(), at_goal(), and their negation.

# How to use a while loop and an if statement.

# Your program should also be valid for worlds Hurdles 1 and Hurdles 2.

 

def turn_right():

    turn_left()

    turn_left()

    turn_left()

   

def jump():

    turn_left()

    move()

    turn_right()

    move()

    turn_right()

    move()

    turn_left()

   

def move_and_jump():

    move()

    jump()

 

while not at_goal():

    if front_is_clear():

        move()

    else:

        jump()

 

Reeborg Hurdle 4:


My Code:

def turn_right():

    turn_left()

    turn_left()

    turn_left()

   

def jump():

    turn_left()

    while is_facing_north() and wall_on_right():

        move()

    if is_facing_north() and right_is_clear():

        turn_right()

        if front_is_clear():

            move()

            if front_is_clear() and right_is_clear():

                turn_right()

 

while not at_goal():

    if is_facing_north() and right_is_clear():

        turn_right()

    elif front_is_clear():

        move()

    else:

        jump()

it is nerve racking watching Reeborg move.

I could not complete the maze..

My Incomplete Code:

# Lost in a maze

# Reeborg was exploring a dark maze and the battery in its flashlight ran out.

 

# Write a program using an if/elif/else statement so Reeborg can find the exit.

# The secret is to have Reeborg follow along the right edge of the maze, turning right if it can, going straight ahead if it can’t turn right, or turning left as a last resort.

 

# What you need to know

# The functions move() and turn_left().

# Either the test front_is_clear() or wall_in_front(), right_is_clear() or wall_on_right(), and at_goal().

# How to use a while loop and if/elif/else statements.

# It might be useful to know how to use the negation of a test (not in Python).

 

def turn_right():

    turn_left()

    turn_left()

    turn_left()

   

while not at_goal():

    while front_is_clear():

        move()

    if wall_in_front():

        turn_right()

    else:

        turn_left()

# of course, it does not work but it is sad that I could not finish.

Done. :)

 

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)