Python Day 6_Functions and Karel
Functions
We can define our function with def -> def my_function(): with indentation
def my_function():
print(“hello”)
print(“bye”)
my_function() -> it will run the defined function
The hurdle loop challenge (https://reeborg.ca/)
Indentation
Only the indented codes in the function will be registered. Rest of the code outside the indentation will be considered independent. There are two ways to indent, Space and tabs. For proper indentation space of 4 is needed and tab can be used when setting is changed.
While loops
Loops that will continue if the conditions are met. The difference between for loop and while loop:
The Hurdle loop2 Challenge (using While loops)
Infinite loop
If there is a while loop that never ends it becomes an infinite loop which can be dangerous. If you have trouble detecting or figuring out the infinite loop, try printing out the conditions to check if its true or false.
The Hurdle loop3 Challenge
Shorter code:
while not at goal():
if wall_in_front():
hurdle()
else:
move()
The hurdle loop4 Challenge
Alternate code:
Project 6: Escaping the maze (Random map)
However if the bot is in a certain position and facing a certain direction the function will cause it to be stuck in an infinite loop.
'Coding : Python' 카테고리의 다른 글
Python_Day 8 (1) | 2023.12.07 |
---|---|
Python_Day 7 (0) | 2023.12.02 |
Python_Day 5 (1) | 2023.11.23 |
Python_Day 4 (0) | 2023.11.17 |
Python_Day 3 (0) | 2023.11.15 |