查看: 2724|回复: 0

[教程] 【一网打尽】Raspberry Pi Week 7 – Day 1 (Beginning Programming)

[复制链接]
  • TA的每日心情
    开心
    2016-8-4 10:56
  • 签到天数: 242 天

    连续签到: 1 天

    [LV.8]以坛为家I

    发表于 2015-3-29 15:55:27 | 显示全部楼层 |阅读模式
    分享到:
    本帖最后由 木林森X 于 2015-3-29 15:58 编辑

    The Raspberry Pi offers a range of programming languages that include Python, C, C++, Ruby, Tiny Basic, assembly and more. Along with these languages come a host tools that allow you to program and examine code on the Raspberry Pi. With so many languages available which one should you select? This is normally a matter of preface or a matter of necessity. For example, PHP is used as a programming language for creating dynamic web pages on the internet.

    Although PHP can be used from the command line its real power is displayed on the web. If you want a fast powerful language then you would use some type of assembly language because it is faster. This does however come with a price and that price is that it is harder to understand.

    Python is a great language because it is powerful and at the same time is very easy to understand. Python has a built in program called IDLE which is an integrated development environment (IDE). This will allow you to program directly into it and see the results line by line. This has the advantage of seeing an error before you out it into a file.

    With other languages you will create a file which is called the source file and this source code is compiled into an executable program. This program is executed and the results are displayed when the program is running.
    Programming languages have some basic common traits which include syntax, variables, control structures and data structures.

    The Syntax
    The syntax is the structure of statements in a computer language much like the arrangement of words in a sentence. They must appear in the correct order in order for the Raspberry Pi to understand what to do.

    For example,
    Stand up, turn 90 degrees to the left and walk 10 paces forward.
    If this appears in this order
    Turn 90 degrees to the left, walk 10 paces and stand up.
    You can instantly see at the end of the sentence that the person was not standing up so how did they manage to walk 10 paces to the left.

    Variables
    A variable is used to store some type of value from numbers, text strings, space ships, house location, credit card numbers and ID numbers to name just a few. A variable is usually named with something meaningful that relates to the value being stored.
    For example if we are programming a computer game and you are storing the number of lives left in a game then a variable may look like this.

    Lives = 3
    This is assigning the number 3 to a variable called Lives. Another example may be that you want to store a person’s name.
    Name = “Homer Simpson”

    Control Structures
    Control decisions are used to determine which direction the program will take. For example in a role playing adventure game you may we arrive at a cross roads and you have the option to turn left or right. Based on your decision the program will take a different path in the code. Here is an example.

    if the direction is left then
    turn left
    walk down the mountain
    else if the direction is right then
    turn right
    walk up the mountain

    Another example might be to allow a player to upgrade their car if they have enough money.

    If money is greater than 100
    allow the player to buy the new car
    These control structures can also be combined for example,

    If money is greater than 100 and the player is female then
    allow the player to walk across the bridge

    Another type of control structure is a loop. A loop is a repetitive block of code that repeats and may exit when a certain event occurs. For example a person is only allowed to purchase food from a takeaway food outlet when their total spend is greater than $10 dollars.

    While the cost of food is less than $9
         Buy a burger at $2
    Take the food

    When the person has purchased 5 burgers they are able to take the food away. Each time a burger is purchased is adds $2.
    The first pass through the loop is $2. The next pass is $4, $6, $8 and on the final pass it will become $10 dollars. When the cost becomes greater than $9 dollars it will exit.

    Data Structures
    A data structure is a way of storing organised data making it easy to access. For example to describe a house address you will need to specific way to store this data. Take a look at the example below.

    Street number = 17
    Street Name = “Harlow Avenue”
    City = “London”
    Post Code = “SW1”
    Country=”UK”

    This data could be contained in a data structure labelled House address or Location. This type of structure really comes into play when we use Arrays. Arrays are used to store a list of similar items just like the address above. Take a look at the image below to understand why data structures are powerful.

    raspberry-pi-programming-data-structures.png


    You can see how the data structure is common across the house locations and a way to access each house location in by calling upon its number.  If you want to retrieve the data in Paris, France then you could write.

    Get House Location [3]

    Data structures will appear often so if at this stage it’s a little fuzzy don’t worry you will understand them over time.

    Debugging
    Writing programs are never simple.  You will at some point have a bug.  A bug is a problem with the code you isn’t immediate apparent and therefore will have to go through the section of code that is causing the problem.

    Take the following example which should allow Jane to take out money from her bank account.

    Input_amount  = 10

    if input_amount  greater than 5 then

    dispense £5 from the account

    Take a look at the above code to see if you can spot the problem.  Jane will only ever receive £5 if the amount is greater than 5 even though her input amount for £10.  Debugging is an art form and you will learn more as your begin programming.  Programming and debugging go hand in hand.

    raspberry-pi-ssh-putty.png
    回复

    使用道具 举报

    您需要登录后才可以回帖 注册/登录

    本版积分规则

    关闭

    站长推荐上一条 /1 下一条



    手机版|小黑屋|与非网

    GMT+8, 2024-6-3 11:12 , Processed in 0.112520 second(s), 16 queries , MemCache On.

    ICP经营许可证 苏B2-20140176  苏ICP备14012660号-2   苏州灵动帧格网络科技有限公司 版权所有.

    苏公网安备 32059002001037号

    Powered by Discuz! X3.4

    Copyright © 2001-2024, Tencent Cloud.