Martin O'Hanlon

Martin O'Hanlon

I love technology and creating projects and learning resources for Raspberry Pi. As a child I wanted to be either a computer scientist, astronaut or snowboard instructor.

Location UK

Activity

  • The program is trying to print the result variable which is an integer.

    result = input1 + input2
    output = str(result)
    print("Answer = " + result + ", well done we merged two numbers.")
    

    The error is stating that Python add (concatenate) a string and an integer.

    You could fix it by printing the output variable which is cast as a…

  • Me too… You may have noticed the odd mistake I make in the videos to this course. They are left in intentionally.

  • You have a function and a variable which are both called quit. When you run quit = False I suspect it removes the reference to your function.

    You should give them distinct names.

  • I dont think Trinket includes a debugger.

  • Not as written but a key advantage of this approach is being able to test to see if how_many actually is a number before casting it to an integer.

    how_many = input("How many items of shopping do you have? "))
    # check `how_many` is actually a number
    how_many = int(how_many)
    

    Data validation and verification isnt covered until our Programming…

  • The program would have continued and the robot steped left before evaluating the while loop’s condition and then exiting.

  • I like the use of nested ifs to make more complex decisions.

  • @RichardGartside I am just doing a review of the course content. Thanks for noting the issue. I think it is just a typo and as @JohnDavis states it isnt needed to complete the task.

  • @SilverBoard Getting ready to run a program is often the hardest part in getting started. C++ is a very mature language with lots of different options for running the code which is to its benefit. Unfortunately, more options doesnt mean its simpler or easier!

  • An excellent use case.

    Interestingly the last digit of a credit card is a “check digit” which is calculated from the previous digits using the modulo 10 algorithm. This is a really simple validation check you can do to see if a credit card number has been entered correctly.

    https://en.wikipedia.org/wiki/Luhn_algorithm

  • I wonder how often you need the very last one? I can’t think of an application for this yet.

    It feels like a common use case and certainly one I use often.

    How about:

    • checking to see it a sentence ends in some appropriate puncutation (e.g. carriage return, full stop)
    • removing redundant spaces from the end of a string
  • There is no symbol for pi but you can get the value using the “math” library.

    You would include this code at the start of your program to import pi

    from math import pi
    

    You could then use it in your program

    res_cir = number1 * 3.14159 * 2
    

    would be

    res_cir = number1 * pi * 2
    
  • That sounds like a great use case for automating using a program.

  • @PaulWilliams I suspect it would return the index of the first item.

  • @WilfriedMestre

    3.4 and -3 for exemple aren’t accepted as numbers, because of the “.”and “-“, wich are not numeric symbols I suppose.

    Yes, the alternative approach is to try and cast the string to a float or int and catch the error.

  • @MakotoHirayama apologies, I got my programming languages mixed up. In Python you have to “escape” the double quote character using a backslash e.g.

    print("My name is \"Martin\", nice to meet you.")
    
  • Is there a command to check if the input is a number or not and redirect to the (error) line if not ?

    There are many ways of doing this. Error detection and correction is introduced in programming 103.

    Conceptually its a relatively difficult concept and requires the use of iteration and abstraction, concepts not covered until later in this course and…

  • since the opening paragraph said to get it to subtract.

    Yes, sorry about that. We noticed the error when doing our review of the course. I have fixed it in the next run.

  • Yes, this would work. I would be cautious of using this in very big lists. I suspect .index(eachitem) would search the list everytime.

  • I dont think so when iterating through a list using for element in list.

    In later steps you will / have learnt about using range() an alternativce approach would be.

    for item_index in range(len(fruitbowl)):
        print("Fruit Bowl position " + str(item_index) + " is a " + fruitbowl[item_index])
    

    Whether it is any “easier” is…

  • int will use convert floats to the lowest whole number e.g.

    int(5.678) will output 5

  • You can print single quote ' characters by including this inside double quotes " (and vice versa) e.g.

    print("My name is 'Martin', nice to meet you.")
    

    You can print double quote when inside double quotes by using pairs e.g.

    print("My name is ""Martin"", nice to meet you.")
    

    Run this code and have a look at the…

  • Hi Paul,

    Have a look at this change I have made to your program. Try and predict what you think will happen before running it. I would be interested in knowing your thoughts on how you think it works.

    ~~~python
    phrase = input(“Talk to me>”)
    if phrase.lower() == “hi”:
    print(“Hello, hope you are well.”)
    else :
    print(“Sorry, I don’t understand,…

  • Thank you. Reflection is an important step and the data in the form really helps us improve our courses.

  • If I was calculating percentages I would take the same approach. I am pretty sure there isnt a built-in function, operator or data type for percentage (e.g. like you would find in a spreadsheet).

  • UItimately I think everything is…

  • Yes, great insight. if then else is introduced in the next step.

  • Those are the 2 obvious / most appropriate changes.

  • In the [previous step](https://www.futurelearn.com/courses/programming-with-guis/10/steps/1293008) there are instructions for installing the guizero library.

  • The data about what colours the GUI is using arent saved in the file. The file just contains the text.

  • At the Raspberry Pi Foundation PRIMM is used when teaching programming. PRIMM was developed by our colleague Sue Sentance.

  • Is there an advantage to using ‘item’ (e.e. it is a shorter word)?

    Not really.

    I chose the variable name item because [in my mind] you have items in shopping lists.

    The choice of variables names is important and picking namees whihch are relevant can help in reading code. Ultimately though is a choice there is no right or wrong answer.

  • I suppose only you can make that decision. The warning is displayed because the Mu installation isnt “signed”. You can read more about Mu at https://codewith.mu/

    It is an open source IDE can you can view the code (if you were so interested) at https://github.com/mu-editor/mu

  • but fun

  • There is a syntax error with this line of code

    use_input="We are going to calcolate the percentage of an items":
    

    There is a colon at the end which isnt required.

  • @vuanmaharani If the music stopped while the loop was running, the code inside the loop would continue until the condition was tested again.

    What do you think would happen if the music stopped playing after the robot had stepped right, but before it had stepped left?

    The robot would finish the loop by stepping left and then stop

  • Great experimentation. You will learn about indentation and scope (what code belongs to what) in a couple of steps time.

  • @PetyaDimitrova re your JavaScript code. When I click on the “Submit” button on the quiz.html page the alert appears. Where you expecting something different to happen?

  • I dont beleive so.

  • It comes down to how Python evaluates (decides if a condition is a True or False).

    This line of code:

    if phrase == "hi" or "hey"
    

    … includes 2 conditions:

    phrase == "hi"
    
    "hey"
    

    phrase == "hi" can be tested and the result will either be True or False depending on whether phrase is equal to “hi” or…

  • Nested ifs are absolutely ok :)

  • @PetyaDimitrova Unfortunately I still see a blank program when I visit the link.

    However, as you are getting this error on line 1, I think it is because you have created a new program with just this code in it.

    This program is meant to be an extension to the bot program you have created during the code.

    You are getting a syntax error because and…

  • @PetyaDimitrova Hi, I followed the link but it was an empty program.

    Can you expand on what wasnt working? Did you experience a syntax error?

  • That is a nice adaptation. Well done.

  • Another benefit is that it doesn’t matter how long the string is, phrase[-1] will always return the last character.

  • You could certainly do that e.g.

    how_many=int(input(“how many items of shoping do you have”))
    print(how_many)
    
  • @PetyaDimitrova The program as it should.

    The for loop will run print(item) for every element in the list.

    Try experimenting by changing the items in shopping_list.

    e.g.

    shopping=[“bred”,”cheese”,”apple”,”biscuits”,”orange”]

    and

    shopping=[“bred”,”cheese”]

    You will see that the print(item) is run once for each item in the list.

  • I needed access to the document.

  • Later in the course you will learn about different types of layout. The “grid” layout is particularly useful for forms.

  • I think you may have shared the wrong program. When I visit this link I see code from a previous step in the course.

  • Can you expand on what you mean please?

    It is this line within the for loop which prints the items in the list.

        print(item)
    

    This line of code will get called multiple times. Once for every item in the list.

  • No. Mu is an IDE for “computers” only. There are Python IDEs for Android and iOS but I have had mixed success and dont have a recommendation.

  • But I couldn’t able to display items through the loop

    Was there a particularly problem you were trying to solve? I ran the program, it seemed to display the items in the list.

  • Stubborn!

    That is a pretty good description of computers and programming languages.

  • Python does expose operators as objects which can be stored as a variable via the [operator module](https://docs.python.org/3/library/operator.html).

    It gets complicated pretty quickly though and the scope is outside that of this course.

  • I like how you investigated what the issue may be.

  • Great work. I just noticed a really minor issue in the code.

    txt_pwd2 = TextBox(app)
    txt_pwd1.hide_text=True
    

    txt_pwd2 is not hidden.

  • Welcome back Rob

  • Hi Petya, I ran your program, added 3 numbers and multiplied 2 others and it gave me the right result. Did you experience a different problem?

  • Be mindful of including brackets when calling print e.g.

    print “oh hi” , yourname , “,I like taylor swift music”
    

    should be:

    print(“oh hi” , yourname , “,I like taylor swift music”)
    

    If you are using trinket you may find that this code would “work”. Trinket has a “useful” feature where it will run Python 2 (where this would have…