SlideShare a Scribd company logo
1 of 19
Core Principles
of Programming
with python
Week 3
Main Menu
A main menu is what is displayed at the
start of any program.
It should be kept in a function and
mainly includes an IF statement.
An example of a main menu using last weeks
code is:
Remembering to use .upper() for validation.
Activity
Using what you have just learnt,
Help
• Remember to use IF and ELIF
statements for each option as
the ELSE statement should be
used if the user enters anything
that isn’t an option.
• Also remember to use .upper()
as a basic form of validation.
Create a main menu for your code from last week.
First Project Layout To achieve this, we will be using the
import command to import into our
project, our first Python library.
The layout of the project will be:
- Main Menu
- Rock Paper Scissors against AI
- Tic Tac Toe against either AI or another player
Setup Activity
Before we get ahead of ourselves:
Further activity
Create a new folder called “Python Game Project”.
Create a new file called “main.py”
Create a function for the main menu with three options:
• Rock Paper Scissors
• Tic Tac Toe
• Exit
Create two functions:
• rockpaperscissors()
• tictactoe()
The main menu should call these functions respectively
Try and place the main menu in a while loop for
validation, that is only exited if the user inputs the
exit choice of the menu.
The main menu should look like this
Console output:
The Further activity main menu should look like this
Console output:
Random Library
A library is a collection of prewritten python
functions that we can use.
The one we will be using today is the “random”
library.
Standard practice is to place all your imports at the
top of the code.
Random.randint() is the command to pick a number
between the first integer and the second integer
(including both of them).
An example of the random and random.randint()
code is:
This code will print a random number of either 1,
2, or 3.
Rock Paper Scissors
Now is the time to flex your skills.
I will let you guys crack on but here is how I would
lay out the program:
• Import random
• Ask the user what they would like to pick
• Print what the computer decided on (1 = Rock, 2
= Paper, 3 = Scissors)
• Compare the two values
• Print if it is a win, lose or draw
• Ask the user to play again (optional)
• Return to main menu
Help
• Compare the users input with
numbers to make your life easier
• Remember “AND” for comparing
values in the IF statement.
• There will be a lot of IF
statements in this program so do
not worry if you have a lot.
• Also remember to use .upper()
as a basic form of validation.
Rock Paper Scissors
For more help here is the comparison code:
• Import random
• Ask the user what they would like to pick
• compChoice = Randint(1,3)
• Convert that to a words
• Print what the computer decided on (1 = Rock, 2
= Paper, 3 = Scissors)
• Compare the two values
• Print if it is a win, lose or draw
• Ask the user to play again (optional)
• Return to main menu
Help
Rock Paper Scissors Here is the full code:
Arrays
An array is a special variable that can be used to
store multiple values in one single variable.
To access a value in the array you would write
the arrays name and then the index of the value
you want in square brackets.
An example of an array is:
0 1 2 3
This would print the word “Is”
as the first value of the array is
called 0.
Tic Tac Toe
Now this is the ultimate challenge.
How I would go about it:
• Create a Board Array to store what spots are empty.
• PrintTable() Function
• Pick which player one, will be (X or O)
• Compare the win conditions of the slots
• When the board is filled, print it was a draw.
• Ask the user to play again (optional)
• Return to main menu
Help
• Print the table with “–” and “|”
• Use board[x] to pick a certain
value in the list.
• Having the win conditions in a
test function will help you
repeatedly check if anyone has
won.
• Also remember to use numbers
for the nine slots.
Tic Tac Toe
Here is some code for printing the
board:
How I would go about it:
• Create a Board Array to store what spots are empty.
• PrintTable() Function
• Pick what player one will be (X or O)
• Compare the win conditions of the slots
• When the board is filled, print it was a draw.
• Ask the user to play again (optional)
• Return to main menu
Help
Tic Tac Toe
Here is some code for validation:
Help
The win conditions for Tic-Tac-Toe:
Tic Tac Toe
Here is the code for entering a position:
Tic Tac Toe
Here is the code for:
• Checking if anyone has won
• Swapping between players
• And asking the user if they want to play again.
Home Activity
Combining what you have learnt today,
Try and make a computer opponent version of Tic-Tac-Toe
using randint() to pick their turns.
End of week Three
And have a good Christmas

More Related Content

Similar to Week3.pptx

Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...DRVaibhavmeshram1
 
PYTHON PROGRAMMING.pptx
PYTHON PROGRAMMING.pptxPYTHON PROGRAMMING.pptx
PYTHON PROGRAMMING.pptxswarna627082
 
Functions, List and String methods
Functions, List and String methodsFunctions, List and String methods
Functions, List and String methodsPranavSB
 
Core Concept_Python.pptx
Core Concept_Python.pptxCore Concept_Python.pptx
Core Concept_Python.pptxAshwini Raut
 
Q-Step_WS_02102019_Practical_introduction_to_Python.pdf
Q-Step_WS_02102019_Practical_introduction_to_Python.pdfQ-Step_WS_02102019_Practical_introduction_to_Python.pdf
Q-Step_WS_02102019_Practical_introduction_to_Python.pdfMichpice
 
Mastering Python lesson 3a
Mastering Python lesson 3aMastering Python lesson 3a
Mastering Python lesson 3aRuth Marvin
 
Basic Python Programming: Part 01 and Part 02
Basic Python Programming: Part 01 and Part 02Basic Python Programming: Part 01 and Part 02
Basic Python Programming: Part 01 and Part 02Fariz Darari
 
Lecture-2-Python-Basic-Elements-Sep04-2018.pptx
Lecture-2-Python-Basic-Elements-Sep04-2018.pptxLecture-2-Python-Basic-Elements-Sep04-2018.pptx
Lecture-2-Python-Basic-Elements-Sep04-2018.pptxAbdulQadeerBilal
 
python-160403194316.pdf
python-160403194316.pdfpython-160403194316.pdf
python-160403194316.pdfgmadhu8
 
Python Seminar PPT
Python Seminar PPTPython Seminar PPT
Python Seminar PPTShivam Gupta
 
Week 2 iLab TCO 2 — Given a simple problem, design a solutio.docx
Week 2 iLab TCO 2 — Given a simple problem, design a solutio.docxWeek 2 iLab TCO 2 — Given a simple problem, design a solutio.docx
Week 2 iLab TCO 2 — Given a simple problem, design a solutio.docxmelbruce90096
 
Looping in PythonLab8 lecture slides.pptx
Looping in PythonLab8 lecture slides.pptxLooping in PythonLab8 lecture slides.pptx
Looping in PythonLab8 lecture slides.pptxadihartanto7
 

Similar to Week3.pptx (20)

Basic use of Python
Basic use of PythonBasic use of Python
Basic use of Python
 
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
 
PYTHON PROGRAMMING.pptx
PYTHON PROGRAMMING.pptxPYTHON PROGRAMMING.pptx
PYTHON PROGRAMMING.pptx
 
Functions, List and String methods
Functions, List and String methodsFunctions, List and String methods
Functions, List and String methods
 
python_class.pptx
python_class.pptxpython_class.pptx
python_class.pptx
 
Cc code cards
Cc code cardsCc code cards
Cc code cards
 
Core Concept_Python.pptx
Core Concept_Python.pptxCore Concept_Python.pptx
Core Concept_Python.pptx
 
Q-Step_WS_02102019_Practical_introduction_to_Python.pdf
Q-Step_WS_02102019_Practical_introduction_to_Python.pdfQ-Step_WS_02102019_Practical_introduction_to_Python.pdf
Q-Step_WS_02102019_Practical_introduction_to_Python.pdf
 
Mastering Python lesson 3a
Mastering Python lesson 3aMastering Python lesson 3a
Mastering Python lesson 3a
 
Basic Python Programming: Part 01 and Part 02
Basic Python Programming: Part 01 and Part 02Basic Python Programming: Part 01 and Part 02
Basic Python Programming: Part 01 and Part 02
 
ACM init() Spring 2015 Day 1
ACM init() Spring 2015 Day 1ACM init() Spring 2015 Day 1
ACM init() Spring 2015 Day 1
 
Lecture-2-Python-Basic-Elements-Sep04-2018.pptx
Lecture-2-Python-Basic-Elements-Sep04-2018.pptxLecture-2-Python-Basic-Elements-Sep04-2018.pptx
Lecture-2-Python-Basic-Elements-Sep04-2018.pptx
 
Week 1.pptx
Week 1.pptxWeek 1.pptx
Week 1.pptx
 
python-160403194316.pdf
python-160403194316.pdfpython-160403194316.pdf
python-160403194316.pdf
 
Python basics
Python basicsPython basics
Python basics
 
Python Seminar PPT
Python Seminar PPTPython Seminar PPT
Python Seminar PPT
 
Python
PythonPython
Python
 
Week 2 iLab TCO 2 — Given a simple problem, design a solutio.docx
Week 2 iLab TCO 2 — Given a simple problem, design a solutio.docxWeek 2 iLab TCO 2 — Given a simple problem, design a solutio.docx
Week 2 iLab TCO 2 — Given a simple problem, design a solutio.docx
 
Baabtra.com little coder chapter - 2
Baabtra.com little coder   chapter - 2Baabtra.com little coder   chapter - 2
Baabtra.com little coder chapter - 2
 
Looping in PythonLab8 lecture slides.pptx
Looping in PythonLab8 lecture slides.pptxLooping in PythonLab8 lecture slides.pptx
Looping in PythonLab8 lecture slides.pptx
 

Recently uploaded

Theoretical Framework- Explanation with Flow Chart.docx
Theoretical Framework- Explanation with Flow Chart.docxTheoretical Framework- Explanation with Flow Chart.docx
Theoretical Framework- Explanation with Flow Chart.docxAman119787
 
Nadiad call girls 📞 8617370543 At Low Cost Cash Payment Booking
Nadiad call girls 📞 8617370543 At Low Cost Cash Payment BookingNadiad call girls 📞 8617370543 At Low Cost Cash Payment Booking
Nadiad call girls 📞 8617370543 At Low Cost Cash Payment BookingNitya salvi
 
Nehru Nagar, Call Girls ☎️ ((#9711106444)), 💘 Full enjoy Low rate girl💘 Genui...
Nehru Nagar, Call Girls ☎️ ((#9711106444)), 💘 Full enjoy Low rate girl💘 Genui...Nehru Nagar, Call Girls ☎️ ((#9711106444)), 💘 Full enjoy Low rate girl💘 Genui...
Nehru Nagar, Call Girls ☎️ ((#9711106444)), 💘 Full enjoy Low rate girl💘 Genui...delhimunirka15
 
Call Girls Varanasi Just Call 8617370543Top Class Call Girl Service Available
Call Girls Varanasi Just Call 8617370543Top Class Call Girl Service AvailableCall Girls Varanasi Just Call 8617370543Top Class Call Girl Service Available
Call Girls Varanasi Just Call 8617370543Top Class Call Girl Service AvailableNitya salvi
 
Call Girls In Dwarka Mor | Contact Me ☎ +91-9953040155
Call Girls In Dwarka Mor | Contact Me ☎ +91-9953040155Call Girls In Dwarka Mor | Contact Me ☎ +91-9953040155
Call Girls In Dwarka Mor | Contact Me ☎ +91-9953040155SaketCallGirlsCallUs
 
Faridabad $ best call girls in Faridabad (Adult Only) 8168257667 Escort Servi...
Faridabad $ best call girls in Faridabad (Adult Only) 8168257667 Escort Servi...Faridabad $ best call girls in Faridabad (Adult Only) 8168257667 Escort Servi...
Faridabad $ best call girls in Faridabad (Adult Only) 8168257667 Escort Servi...Hyderabad Escorts Agency
 
Call Girls Aligarh Just Call 8617370543 Top Class Call Girl Service Available
Call Girls Aligarh Just Call 8617370543 Top Class Call Girl Service AvailableCall Girls Aligarh Just Call 8617370543 Top Class Call Girl Service Available
Call Girls Aligarh Just Call 8617370543 Top Class Call Girl Service AvailableNitya salvi
 
Kathmandu Escort❤ @Daminy@💞 50+ Call Girl PRofile in @Kathmandu New Housewife...
Kathmandu Escort❤ @Daminy@💞 50+ Call Girl PRofile in @Kathmandu New Housewife...Kathmandu Escort❤ @Daminy@💞 50+ Call Girl PRofile in @Kathmandu New Housewife...
Kathmandu Escort❤ @Daminy@💞 50+ Call Girl PRofile in @Kathmandu New Housewife...Miss Beniwal
 
Call Girls In Porbandar 📞 8617370543 At Low Cost Cash Payment Booking
Call Girls In Porbandar 📞 8617370543  At Low Cost Cash Payment BookingCall Girls In Porbandar 📞 8617370543  At Low Cost Cash Payment Booking
Call Girls In Porbandar 📞 8617370543 At Low Cost Cash Payment BookingNitya salvi
 
Jaunpur Escorts Service Girl ^ 9332606886, WhatsApp Anytime Jaunpur
Jaunpur Escorts Service Girl ^ 9332606886, WhatsApp Anytime JaunpurJaunpur Escorts Service Girl ^ 9332606886, WhatsApp Anytime Jaunpur
Jaunpur Escorts Service Girl ^ 9332606886, WhatsApp Anytime Jaunpurmeghakumariji156
 
Call Girls Ahwa Just Call 📞 8617370543 Top Class Call Girl Service Available
Call Girls Ahwa Just Call 📞 8617370543 Top Class Call Girl Service AvailableCall Girls Ahwa Just Call 📞 8617370543 Top Class Call Girl Service Available
Call Girls Ahwa Just Call 📞 8617370543 Top Class Call Girl Service AvailableNitya salvi
 
Completed Event Presentation for Huma 1305
Completed Event Presentation for Huma 1305Completed Event Presentation for Huma 1305
Completed Event Presentation for Huma 1305jazlynjacobs51
 
SB_ Pretzel and the puppies_ Rough_ RiverPhan (2024)
SB_ Pretzel and the puppies_ Rough_ RiverPhan (2024)SB_ Pretzel and the puppies_ Rough_ RiverPhan (2024)
SB_ Pretzel and the puppies_ Rough_ RiverPhan (2024)River / Thao Phan
 
Azad Nagar Call Girls ,☎️ ((#9711106444)), 💘 Full enjoy Low rate girl💘 Genuin...
Azad Nagar Call Girls ,☎️ ((#9711106444)), 💘 Full enjoy Low rate girl💘 Genuin...Azad Nagar Call Girls ,☎️ ((#9711106444)), 💘 Full enjoy Low rate girl💘 Genuin...
Azad Nagar Call Girls ,☎️ ((#9711106444)), 💘 Full enjoy Low rate girl💘 Genuin...delhimunirka15
 
Premium Call Girls Ranchi 0000000000 ❤VVIP POOJA Call Girls in Ranchi Jharkhand
Premium Call Girls Ranchi 0000000000 ❤VVIP POOJA Call Girls in Ranchi JharkhandPremium Call Girls Ranchi 0000000000 ❤VVIP POOJA Call Girls in Ranchi Jharkhand
Premium Call Girls Ranchi 0000000000 ❤VVIP POOJA Call Girls in Ranchi JharkhandCall Girls Mumbai
 
Call Girls Bhavnagar - 📞 8617370543 Our call girls are sure to provide you wi...
Call Girls Bhavnagar - 📞 8617370543 Our call girls are sure to provide you wi...Call Girls Bhavnagar - 📞 8617370543 Our call girls are sure to provide you wi...
Call Girls Bhavnagar - 📞 8617370543 Our call girls are sure to provide you wi...Nitya salvi
 
Call girl Jaipur (service) 9257505146,9216459764
Call girl Jaipur (service) 9257505146,9216459764Call girl Jaipur (service) 9257505146,9216459764
Call girl Jaipur (service) 9257505146,9216459764Jaipur
 
Faridabad Call Girls 💯Call Us 🔝 +91-8168257667 🔝 💃 Top Class Russian Faridaba...
Faridabad Call Girls 💯Call Us 🔝 +91-8168257667 🔝 💃 Top Class Russian Faridaba...Faridabad Call Girls 💯Call Us 🔝 +91-8168257667 🔝 💃 Top Class Russian Faridaba...
Faridabad Call Girls 💯Call Us 🔝 +91-8168257667 🔝 💃 Top Class Russian Faridaba...Hyderabad Escorts Agency
 
Russian Call Girls New Kolkata Whatsapp Numbers 0000000000 Russian Escorts S...
Russian Call Girls New Kolkata Whatsapp Numbers 0000000000  Russian Escorts S...Russian Call Girls New Kolkata Whatsapp Numbers 0000000000  Russian Escorts S...
Russian Call Girls New Kolkata Whatsapp Numbers 0000000000 Russian Escorts S...Call Girls Mumbai
 
Khambhalia Escorts 8617370543 Khambhalia Call Girls Service
Khambhalia Escorts 8617370543 Khambhalia Call Girls ServiceKhambhalia Escorts 8617370543 Khambhalia Call Girls Service
Khambhalia Escorts 8617370543 Khambhalia Call Girls ServiceNitya salvi
 

Recently uploaded (20)

Theoretical Framework- Explanation with Flow Chart.docx
Theoretical Framework- Explanation with Flow Chart.docxTheoretical Framework- Explanation with Flow Chart.docx
Theoretical Framework- Explanation with Flow Chart.docx
 
Nadiad call girls 📞 8617370543 At Low Cost Cash Payment Booking
Nadiad call girls 📞 8617370543 At Low Cost Cash Payment BookingNadiad call girls 📞 8617370543 At Low Cost Cash Payment Booking
Nadiad call girls 📞 8617370543 At Low Cost Cash Payment Booking
 
Nehru Nagar, Call Girls ☎️ ((#9711106444)), 💘 Full enjoy Low rate girl💘 Genui...
Nehru Nagar, Call Girls ☎️ ((#9711106444)), 💘 Full enjoy Low rate girl💘 Genui...Nehru Nagar, Call Girls ☎️ ((#9711106444)), 💘 Full enjoy Low rate girl💘 Genui...
Nehru Nagar, Call Girls ☎️ ((#9711106444)), 💘 Full enjoy Low rate girl💘 Genui...
 
Call Girls Varanasi Just Call 8617370543Top Class Call Girl Service Available
Call Girls Varanasi Just Call 8617370543Top Class Call Girl Service AvailableCall Girls Varanasi Just Call 8617370543Top Class Call Girl Service Available
Call Girls Varanasi Just Call 8617370543Top Class Call Girl Service Available
 
Call Girls In Dwarka Mor | Contact Me ☎ +91-9953040155
Call Girls In Dwarka Mor | Contact Me ☎ +91-9953040155Call Girls In Dwarka Mor | Contact Me ☎ +91-9953040155
Call Girls In Dwarka Mor | Contact Me ☎ +91-9953040155
 
Faridabad $ best call girls in Faridabad (Adult Only) 8168257667 Escort Servi...
Faridabad $ best call girls in Faridabad (Adult Only) 8168257667 Escort Servi...Faridabad $ best call girls in Faridabad (Adult Only) 8168257667 Escort Servi...
Faridabad $ best call girls in Faridabad (Adult Only) 8168257667 Escort Servi...
 
Call Girls Aligarh Just Call 8617370543 Top Class Call Girl Service Available
Call Girls Aligarh Just Call 8617370543 Top Class Call Girl Service AvailableCall Girls Aligarh Just Call 8617370543 Top Class Call Girl Service Available
Call Girls Aligarh Just Call 8617370543 Top Class Call Girl Service Available
 
Kathmandu Escort❤ @Daminy@💞 50+ Call Girl PRofile in @Kathmandu New Housewife...
Kathmandu Escort❤ @Daminy@💞 50+ Call Girl PRofile in @Kathmandu New Housewife...Kathmandu Escort❤ @Daminy@💞 50+ Call Girl PRofile in @Kathmandu New Housewife...
Kathmandu Escort❤ @Daminy@💞 50+ Call Girl PRofile in @Kathmandu New Housewife...
 
Call Girls In Porbandar 📞 8617370543 At Low Cost Cash Payment Booking
Call Girls In Porbandar 📞 8617370543  At Low Cost Cash Payment BookingCall Girls In Porbandar 📞 8617370543  At Low Cost Cash Payment Booking
Call Girls In Porbandar 📞 8617370543 At Low Cost Cash Payment Booking
 
Jaunpur Escorts Service Girl ^ 9332606886, WhatsApp Anytime Jaunpur
Jaunpur Escorts Service Girl ^ 9332606886, WhatsApp Anytime JaunpurJaunpur Escorts Service Girl ^ 9332606886, WhatsApp Anytime Jaunpur
Jaunpur Escorts Service Girl ^ 9332606886, WhatsApp Anytime Jaunpur
 
Call Girls Ahwa Just Call 📞 8617370543 Top Class Call Girl Service Available
Call Girls Ahwa Just Call 📞 8617370543 Top Class Call Girl Service AvailableCall Girls Ahwa Just Call 📞 8617370543 Top Class Call Girl Service Available
Call Girls Ahwa Just Call 📞 8617370543 Top Class Call Girl Service Available
 
Completed Event Presentation for Huma 1305
Completed Event Presentation for Huma 1305Completed Event Presentation for Huma 1305
Completed Event Presentation for Huma 1305
 
SB_ Pretzel and the puppies_ Rough_ RiverPhan (2024)
SB_ Pretzel and the puppies_ Rough_ RiverPhan (2024)SB_ Pretzel and the puppies_ Rough_ RiverPhan (2024)
SB_ Pretzel and the puppies_ Rough_ RiverPhan (2024)
 
Azad Nagar Call Girls ,☎️ ((#9711106444)), 💘 Full enjoy Low rate girl💘 Genuin...
Azad Nagar Call Girls ,☎️ ((#9711106444)), 💘 Full enjoy Low rate girl💘 Genuin...Azad Nagar Call Girls ,☎️ ((#9711106444)), 💘 Full enjoy Low rate girl💘 Genuin...
Azad Nagar Call Girls ,☎️ ((#9711106444)), 💘 Full enjoy Low rate girl💘 Genuin...
 
Premium Call Girls Ranchi 0000000000 ❤VVIP POOJA Call Girls in Ranchi Jharkhand
Premium Call Girls Ranchi 0000000000 ❤VVIP POOJA Call Girls in Ranchi JharkhandPremium Call Girls Ranchi 0000000000 ❤VVIP POOJA Call Girls in Ranchi Jharkhand
Premium Call Girls Ranchi 0000000000 ❤VVIP POOJA Call Girls in Ranchi Jharkhand
 
Call Girls Bhavnagar - 📞 8617370543 Our call girls are sure to provide you wi...
Call Girls Bhavnagar - 📞 8617370543 Our call girls are sure to provide you wi...Call Girls Bhavnagar - 📞 8617370543 Our call girls are sure to provide you wi...
Call Girls Bhavnagar - 📞 8617370543 Our call girls are sure to provide you wi...
 
Call girl Jaipur (service) 9257505146,9216459764
Call girl Jaipur (service) 9257505146,9216459764Call girl Jaipur (service) 9257505146,9216459764
Call girl Jaipur (service) 9257505146,9216459764
 
Faridabad Call Girls 💯Call Us 🔝 +91-8168257667 🔝 💃 Top Class Russian Faridaba...
Faridabad Call Girls 💯Call Us 🔝 +91-8168257667 🔝 💃 Top Class Russian Faridaba...Faridabad Call Girls 💯Call Us 🔝 +91-8168257667 🔝 💃 Top Class Russian Faridaba...
Faridabad Call Girls 💯Call Us 🔝 +91-8168257667 🔝 💃 Top Class Russian Faridaba...
 
Russian Call Girls New Kolkata Whatsapp Numbers 0000000000 Russian Escorts S...
Russian Call Girls New Kolkata Whatsapp Numbers 0000000000  Russian Escorts S...Russian Call Girls New Kolkata Whatsapp Numbers 0000000000  Russian Escorts S...
Russian Call Girls New Kolkata Whatsapp Numbers 0000000000 Russian Escorts S...
 
Khambhalia Escorts 8617370543 Khambhalia Call Girls Service
Khambhalia Escorts 8617370543 Khambhalia Call Girls ServiceKhambhalia Escorts 8617370543 Khambhalia Call Girls Service
Khambhalia Escorts 8617370543 Khambhalia Call Girls Service
 

Week3.pptx

  • 2. Main Menu A main menu is what is displayed at the start of any program. It should be kept in a function and mainly includes an IF statement. An example of a main menu using last weeks code is: Remembering to use .upper() for validation.
  • 3. Activity Using what you have just learnt, Help • Remember to use IF and ELIF statements for each option as the ELSE statement should be used if the user enters anything that isn’t an option. • Also remember to use .upper() as a basic form of validation. Create a main menu for your code from last week.
  • 4. First Project Layout To achieve this, we will be using the import command to import into our project, our first Python library. The layout of the project will be: - Main Menu - Rock Paper Scissors against AI - Tic Tac Toe against either AI or another player
  • 5. Setup Activity Before we get ahead of ourselves: Further activity Create a new folder called “Python Game Project”. Create a new file called “main.py” Create a function for the main menu with three options: • Rock Paper Scissors • Tic Tac Toe • Exit Create two functions: • rockpaperscissors() • tictactoe() The main menu should call these functions respectively Try and place the main menu in a while loop for validation, that is only exited if the user inputs the exit choice of the menu.
  • 6. The main menu should look like this Console output:
  • 7. The Further activity main menu should look like this Console output:
  • 8. Random Library A library is a collection of prewritten python functions that we can use. The one we will be using today is the “random” library. Standard practice is to place all your imports at the top of the code. Random.randint() is the command to pick a number between the first integer and the second integer (including both of them). An example of the random and random.randint() code is: This code will print a random number of either 1, 2, or 3.
  • 9. Rock Paper Scissors Now is the time to flex your skills. I will let you guys crack on but here is how I would lay out the program: • Import random • Ask the user what they would like to pick • Print what the computer decided on (1 = Rock, 2 = Paper, 3 = Scissors) • Compare the two values • Print if it is a win, lose or draw • Ask the user to play again (optional) • Return to main menu Help • Compare the users input with numbers to make your life easier • Remember “AND” for comparing values in the IF statement. • There will be a lot of IF statements in this program so do not worry if you have a lot. • Also remember to use .upper() as a basic form of validation.
  • 10. Rock Paper Scissors For more help here is the comparison code: • Import random • Ask the user what they would like to pick • compChoice = Randint(1,3) • Convert that to a words • Print what the computer decided on (1 = Rock, 2 = Paper, 3 = Scissors) • Compare the two values • Print if it is a win, lose or draw • Ask the user to play again (optional) • Return to main menu Help
  • 11. Rock Paper Scissors Here is the full code:
  • 12. Arrays An array is a special variable that can be used to store multiple values in one single variable. To access a value in the array you would write the arrays name and then the index of the value you want in square brackets. An example of an array is: 0 1 2 3 This would print the word “Is” as the first value of the array is called 0.
  • 13. Tic Tac Toe Now this is the ultimate challenge. How I would go about it: • Create a Board Array to store what spots are empty. • PrintTable() Function • Pick which player one, will be (X or O) • Compare the win conditions of the slots • When the board is filled, print it was a draw. • Ask the user to play again (optional) • Return to main menu Help • Print the table with “–” and “|” • Use board[x] to pick a certain value in the list. • Having the win conditions in a test function will help you repeatedly check if anyone has won. • Also remember to use numbers for the nine slots.
  • 14. Tic Tac Toe Here is some code for printing the board: How I would go about it: • Create a Board Array to store what spots are empty. • PrintTable() Function • Pick what player one will be (X or O) • Compare the win conditions of the slots • When the board is filled, print it was a draw. • Ask the user to play again (optional) • Return to main menu Help
  • 15. Tic Tac Toe Here is some code for validation: Help The win conditions for Tic-Tac-Toe:
  • 16. Tic Tac Toe Here is the code for entering a position:
  • 17. Tic Tac Toe Here is the code for: • Checking if anyone has won • Swapping between players • And asking the user if they want to play again.
  • 18. Home Activity Combining what you have learnt today, Try and make a computer opponent version of Tic-Tac-Toe using randint() to pick their turns.
  • 19. End of week Three And have a good Christmas