# Difference between revisions of "1 The basic algorithm"

(→Enter the year to determine the leap year) |
|||

(10 intermediate revisions by the same user not shown) | |||

Line 1: | Line 1: | ||

+ | [[File:Webduino_gif.gif|thumb|Overview: [[BPI-Bit]]]] | ||

+ | [[File:Micropython3.png|thumb|[[1 Get the development suite]] ]] | ||

+ | [[File:Micropython2.png|thumb|[[2 Wired connection board]] ]] | ||

+ | [[File:Micropython2.png|thumb|[[3 Try burning the board]] ]] | ||

+ | [[File:Micropython2.png|thumb|[[4 Hello, World!]] ]] | ||

+ | [[File:Micropython2.png|thumb|[[5 Light up all kinds of LED]] ]] | ||

+ | |||

+ | |||

+ | [[File:Micropython4.png|thumb|[[1 Auto series one key burn]] ]] | ||

+ | [[File:Micropython2.png|thumb|[[2 ESPBlocks Beginner's only]] ]] | ||

+ | [[File:Micropython2.png|thumb|[[3 Mpfshell details]] ]] | ||

+ | [[File:Micropython2.png|thumb|[[4 Pycharm IDE programming]] ]] | ||

+ | |||

+ | |||

+ | [[File:Micropython5.png|thumb|[[1. Panel scrolling text]] ]] | ||

+ | [[File:Micropython2.png|thumb|[[2. Panel display images]] ]] | ||

+ | [[File:Micropython2.png|thumb|[[3 Bottom IO port control]] ]] | ||

+ | [[File:Micropython2.png|thumb|[[4 Panel key detection]] ]] | ||

+ | [[File:Micropython2.png|thumb|[[5 Gets the board temperature]] ]] | ||

+ | [[File:Micropython2.png|thumb|[[6 Play MIDI music]] ]] | ||

+ | [[File:Micropython2.png|thumb|[[7 Photosensitive gesture]] ]] | ||

+ | [[File:Micropython2.png|thumb|[[8. MPU-9250 9-axis sensor]] ]] | ||

+ | [[File:Micropython2.png|thumb|[[9. Make a compass]] ]] | ||

+ | [[File:Micropython2.png|thumb|[[10 Free to define gestures]] ]] | ||

+ | [[File:Micropython2.png|thumb|[[11 Random number generator]] ]] | ||

+ | [[File:Micropython2.png|thumb|[[12 S2m Scratch2]] ]] | ||

+ | [[File:Micropython2.png|thumb|[[13 Codelab Scratch3]] ]] | ||

+ | |||

+ | |||

+ | [[File:Micropython6.png|thumb|[[1 The basic algorithm]] ]] | ||

+ | [[File:Micropython2.png|thumb|[[2 WiFI wireless connection]] ]] | ||

+ | [[File:Micropython2.png|thumb|[[3 WiFI wireless programming]] ]] | ||

+ | [[File:Micropython2.png|thumb|[[MQTT communication applications]]]] | ||

'''Implement the basic algorithm''' | '''Implement the basic algorithm''' | ||

Line 9: | Line 42: | ||

The code is as follows :(can be placed in main.py): | The code is as follows :(can be placed in main.py): | ||

− | |||

def is_leap_year(year): | def is_leap_year(year): | ||

− | + | if (year % 4) == 0 and (year % 100) != 0 or (year % 400) == 0: | |

− | + | print("{0} is leap year".format(year)) | |

− | + | else: | |

− | + | print("{0} not is leap year".format(year)) | |

− | + | using runfile main.py,Run it in the interpreter and then run commond in repl. | |

is_leap_year(int(input("input a leap year "))) | is_leap_year(int(input("input a leap year "))) | ||

[[File:Mpfshell17.png]] | [[File:Mpfshell17.png]] | ||

+ | |||

+ | *The year 2004 is leap year. | ||

+ | *The year 1999 is not leap year. | ||

+ | *The year 2000 is leap year. | ||

+ | *You can also go ahead and type in more and have it tell you which year is a leap year. | ||

+ | |||

+ | When you type a command in REPL, you are prompted to enter a value, and press ok to confirm the input. | ||

+ | |||

+ | =Fibonacci sequence= | ||

+ | |||

+ | What is a Fibonacci sequence? What it means is that you have a sequence of 0, 1, 1, 2, 3, 5, 8, 13, and in particular, the 0th term is 0, and the first term is the first 1. Starting with the third term, each term is equal to the sum of the first two terms. | ||

+ | |||

+ | The code is as follows :(can be placed in main.py) | ||

+ | |||

+ | def fab(n): | ||

+ | if n == 1: | ||

+ | return 0 | ||

+ | if n == 2: | ||

+ | return 1 | ||

+ | if n > 2: | ||

+ | return fab(n-1) + fab(n-2) | ||

+ | |||

+ | def printfablist(n): | ||

+ | for i in range(1, n+1): | ||

+ | print(fab(i),end = ' ') | ||

+ | print('') | ||

+ | |||

+ | using runfile main.py,Run it in the interpreter and then run commond in repl. | ||

+ | |||

+ | printfablist(int(input('please input a number:')) | ||

+ | |||

+ | [[File:Mpfshell16.png]] | ||

+ | |||

+ | You can try to compute the relationship between the first term and the second term on your own. | ||

+ | |||

+ | When you type a command in REPL, you are prompted to enter a value and press ok for input. For example, if I enter 5, the Fibonacci sequence of 5 items (0, 1, 1, 2, 3) is output. |

## Latest revision as of 02:40, 21 February 2019

**Implement the basic algorithm**

After we learned some basic hardware controls in the last chapter, let's make up our basic software lessons.

# Enter the year to determine the leap year

A leap year is a noun in the Gregorian calendar. Common year: a year that is divisible by 4 but not by 100 is a common leap year. (2004 is a leap year, 1999 is not); Century years: leap years are those divisible by 400.

The code is as follows :(can be placed in main.py):

def is_leap_year(year): if (year % 4) == 0 and (year % 100) != 0 or (year % 400) == 0: print("{0} is leap year".format(year)) else: print("{0} not is leap year".format(year))

using runfile main.py,Run it in the interpreter and then run commond in repl.

is_leap_year(int(input("input a leap year ")))

- The year 2004 is leap year.
- The year 1999 is not leap year.
- The year 2000 is leap year.
- You can also go ahead and type in more and have it tell you which year is a leap year.

When you type a command in REPL, you are prompted to enter a value, and press ok to confirm the input.

# Fibonacci sequence

What is a Fibonacci sequence? What it means is that you have a sequence of 0, 1, 1, 2, 3, 5, 8, 13, and in particular, the 0th term is 0, and the first term is the first 1. Starting with the third term, each term is equal to the sum of the first two terms.

The code is as follows :(can be placed in main.py)

def fab(n): if n == 1: return 0 if n == 2: return 1 if n > 2: return fab(n-1) + fab(n-2) def printfablist(n): for i in range(1, n+1): print(fab(i),end = ' ') print()

using runfile main.py,Run it in the interpreter and then run commond in repl.

printfablist(int(input('please input a number:'))

You can try to compute the relationship between the first term and the second term on your own.

When you type a command in REPL, you are prompted to enter a value and press ok for input. For example, if I enter 5, the Fibonacci sequence of 5 items (0, 1, 1, 2, 3) is output.