0 results for
ICT
Computing
Responsive image

Coding in Python - Chatbots

0 results forGuest
Overview
Flight Path
Learning Objectives
0 results forGuest
IDSkill_name
 4 I can build the first part of an artificial intelligence program using Python
 5 I can demonstrate and explain the practice of commenting my code
 5 I can create a simple maths test, and convert a string to an integer
 5 I can make use of 'if' statements to create a maths quiz

Starter - Task

🏁 Learning Objective 8 :- I can build the first part of an artificial intelligence program using Python


Creating a chatbot

Type the code below into your python IDLE.

print("Please type your name in")
my_name = input()
print("Nice to meet you " + my_name)

RUN your code and test that it works.

Can you add another question and print out another answer?

If you are not sure what to write look at my example below.

Type the code below into your python IDLE.

print("So,"+ my_name + " what is your favourite food?" )
favourite_food = input()
print( "Ah, your favourite food is " + favourite_food)

Update your objectives

control

🏁 Learning Objective 9 :- I can demonstrate and explain the practice of commenting my code


Comment Your Code

Comments are often added to computer programs to allow people to understand the intentions of the person who created the code. The Python interpreter ignores the comments completely, so syntax is not a problem. In practise, it is not necessary to comment every line/section – only where it is not obvious what is going on. However, as you have probably not experienced commenting before, we will apply some comments to the simple code we have written so far using the hash key (#). You will notice that when you use the hash key in the program editor, the text changes to red to indicate the use of commenting. Comments can be used for sections, or in-line as shown below

# This program finds out the user's name
print("Please type your name in")
# prints message to user
my_name = input()
# stores user’s name in a variable
print("Nice to meet you " + my_name)
# displays message

Can you add comments to your code?

Update your objectives

control

🏁 Learning Objective 10 :- I can create a simple maths test, and convert a string to an integer


Creating a maths test question

There is a lot of value in creating games when learning how to program. This part of the lesson starts with a simple question script that when understood can be used to build a more sophisticated game.

Task 1

Open a new program editor window, remember we do this in the interpreter by selecting:

File – New Window

Save this as maths_question.py

Now create the script below, exactly as it appears below.

Questions

1. answer = int(answer) - thinking back to use of integers last lesson, what does this do and what does ‘int’ mean?

Click here for answer

answer - this converts a text string into a number or integer. If we did not convert it to an integer, we could not compare it to another integer, the answer 4.

2. If answer = = 4: - What does the ‘= =’ translate to in plain English?

Click here for answer

Answer- this means ‘equal’ to, as in "is it equal to?"

3. Why are the indents necessary after the if and else statements?

Click here for answer

Answer- this means follow these instructions if the statement above is true

4. What happens if the colons are not there after the 4 or else?

Click here for answer

You get a Syntax Error

Update your objectives

control

🏁 Learning Objective 11 :- I can make use of 'if' statements to create a maths quiz


Creating a maths quiz

Create a test that will check the user’s knowledge of simple maths quiz , with between 4 and 12 questions.

You must save it with the name maths_quiz.py and it must include a header comment, with description, name of creator and date.

The program must feature some other use of comments and must work successfully.

Here is a reminder from last lesson how you might start your quiz

Adding in a score variable

Look at the code below to see how you could create a variable to keep a score of the correct answers.

Outputting the score

Look at the code below to remind you how you could give the user their final score.


Extension task

Can you think of a way that you could tell the user how many questions the user had answered and put that as part of their final score?

For example if you asked 5 questions and they got 4 correct could you output:

"Well done you got 4 out of 5 correct"

Update your objectives

control


0 results forGuest
Recent Comments

Teacher Date: 2020-07-05


Guest