Learning Objectives
Implement voice chat with ASR and TTS
Execute and comprehend single-branch structure
Execute and comprehend double-branch structure
Execute and comprehend multi-branch structure
Master Python syntax: if, elif, else, equal, colon and indentation
(Extra) Understand nested branch structure and operators
Activity 1 Run simple voice chat code
Copy and run the code (silly one)
import edge_tts
import os
import speech_recognition as sr
rec = sr.Recognizer()
mic = sr.Microphone()
print("Please speak English")
mic.__enter__()
audio = rec.listen(mic)
txt = rec.recognize_google(audio, language='en-US')
print("Recognized text:", txt)
voice = "en-US-AriaNeural"
txt = "Nice to meet you!"
tts = edge_tts.Communicate(txt, voice)
path = "tts.mp3"
tts.save_sync(path)
os.startfile(path)Activity 2 Understand the Single-branch Structure
Insert the code 2 into the code 1and run
#code 1
import edge_tts
import os
import speech_recognition as sr
rec = sr.Recognizer()
mic = sr.Microphone()
print("Please speak English")
mic.__enter__()
audio = rec.listen(mic)
speech_text = rec.recognize_google(audio, language='en-US')
print("Recognized text:", speech_text)
reply = ""
voice = "en-US-AriaNeural"
tts = edge_tts.Communicate(reply, voice)
path = "tts.mp3"
tts.save_sync(path)
os.startfile(path)#code 2
speech_text = rec.recognize_google(audio, language='en-US')
if speech_text == "Hello":
reply = "Hello, how are you?"Read and run the single branch and grammar
age = input()
age = int(age)
if age >= 18:
print('Adult')Write a program.
Problem Description
Write a program to judge whether the input score passes the exam.
If the score is greater than or equal to 60, print Pass.
If the score is less than 60, do not print anything.
Input
A single integer representing the exam score (0 ≤ score ≤ 100).
Output
Print Pass only when the score ≥ 60; otherwise, print nothing.
Sample Input 1
60
Sample Output 1
PassActivity 3 Understand the Double-branch Structure
Insert the code 3 into the code 1and run
#code 3
speech_text = rec.recognize_google(audio, language='en-US')
if speech_text == "Hello":
reply = "Hello, how are you?"
else:
reply = "Sorry, I can not understand you."Read and run the double branch and grammar
age = input()
age = int(age)
if age >= 18:
print('Adult')
else:
print("Child")Write a program.
Problem Description
Write a program to judge if a student's exam score is qualified.
A score is considered pass when it is no less than 60.
Input an integer score, print Pass if passed, otherwise print Fail.
Input
Single integer representing exam score.
Output
Output one word: Pass or Fail
Sample Input 1
65
Sample Output 1
Pass
Sample Input 2
58
Sample Output 2
FailActivity 4 Understand the Multiple-branch Structure
Insert the code 4 into the code 1and run
#code 4
speech_text = rec.recognize_google(audio, language='en-US')
if speech_text == "Hello":
reply = "Hello, how are you?"
elif speech_text == "How are you":
reply = "I am fine, thank you."
else:
reply = "Sorry, I can not understand you."Write a program.
Problem Description
Write a program to grade the exam score using multiple conditions.
If score ≥ 90: print A
If score ≥ 80 and < 90: print B
If score ≥ 70 and < 80: print C
If score ≥ 60 and < 70: print Pass
If score < 60: print Fail
Input
A single integer (0 ≤ score ≤ 100)
Output
Print the corresponding grade, only print Pass for 60-69, print Fail if below 60.
Sample Input 1
95
Sample Output 1
A
Sample Input 2
82
Sample Output 2
B
Sample Input 3
65
Sample Output 3
Pass
Sample Input 4
50
Sample Output 4
FailActivity 5 Syntax Learning Test
Read the syntax element below
Syntax Element | Description | Example |
if | Judge the first condition |
|
elif | Judge extra conditions sequentially |
|
else | Execute when all conditions fail |
|
colon | Mark the end of condition line | Add at the end of judgment statement |
indentation | Fixed space offset for code block | Indent inner code uniformly Read the syntax element below |
Read the operators below
Operator | Meaning | Example |
+ | Addition | 5 + 3 |
- | Subtraction | 10 - 4 |
* | Multiplication | 2 * 6 |
/ | Division | 8 / 2 |
** | Exponentiation | 3 ** 2 |
> | Greater than | a > 5 |
>= | Greater than or equal to | a >= 5 |
< | Less than | a < 10 |
<= | Less than or equal to | a <= 10 |
== | Equal to | a == 5 |
!= | Not equal to | a != 3 |
and | Logical AND | a>1 and a<10 |
or | Logical OR | a==2 or a==4 |
not | Logical NOT | not a==0 |
in | Exist in sequence | "hi" in text |
ICT 3 MCQ
Which keyword starts the first conditional judgment?
Which keyword is used for additional conditional checks?
Which part runs when all above conditions are false?
What symbol must be added at the end of a condition line?
What defines a code block in Python branch structure?
How many else blocks can one complete branch structure have?
Which statement is grammatically correct?
Indented code belongs to ______.
The correct order of branch keywords is ______.
When using multiple elif, conditions are judged ______.
Activity 6 Nested Branch and Logical operators
Use nested branch in the code
import edge_tts
import os
import speech_recognition as sr
rec = sr.Recognizer()
mic = sr.Microphone()
print("Please speak English")
mic.__enter__()
audio = rec.listen(mic)
speech_text = rec.recognize_google(audio, language='en-US')
print("Recognized text:", speech_text)
reply = ""
# Use all operators + Nested branch structure
if "Hello" in speech_text or "Hi" in speech_text:
reply = "Hello! How are you?"
elif speech_text == "How are you":
# Nested if + not
if not speech_text != "How are you":
reply = "I am fine, thank you!"
else:
reply = "I'm doing well!"
elif speech_text == "What is your name":
reply = "My name is AI voice robot."
elif speech_text == "Goodbye" and "Bye" not in speech_text:
reply = "See you next time!"
elif speech_text == "Thank you" or speech_text == "Thanks":
reply = "You are welcome!"
elif speech_text != "What":
reply = "Nice to talk with you!"
else:
reply = "Sorry, I cannot understand you."
voice = "en-US-AriaNeural"
tts = edge_tts.Communicate(reply, voice)
path = "tts.mp3"
tts.save_sync(path)
os.startfile(path)