2.1.3.8 Lab – Create a Simple Game with Python IDLE Answers

2.1.3.8 Lab – Create a Simple Game with Python IDLE Answers

Objectives

  • Part 1: Launch VirtualBox and Enter the I2IoT server VM
  • Part 2: Create a Simple Game with Python IDLE
  • Part 3: IDLE for Python

Background

Python, a programming language, allows for simpler statements. Python is very easy to use, powerful, and versatile. It has become the language of choice for many IoT developers. One of the main reasons for the popularity of Python is the developer community; Python developers have created and made available many specific modules that can be imported into any program to immediately lend added functionality.

Scenario

In this lab, you will create a simple game using Python IDLE.

Required Resources

  • A modern personal computer with sufficient RAM and with internet access.
  • VirtualBox with I2IoT server installed.

Instructions

Part 1: Launch VirtualBox and Enter the I2IoT server VM

In Part 1, you launch virtualization software VirtualBox and login to the I2IoT server VM.

Step 1: Launch VirtualBox.

a. After VirtualBox is installed (see Lab 2.1.2.a), the VirtualBox icon should appear on the Desktop. Click the icon to launch the VirtualBox.

2.1.3.8 Lab - Create a Simple Game with Python IDLE Answers 12

b. Click I2IoT – GUI on the left pane to launch the server VM.

2.1.3.8 Lab - Create a Simple Game with Python IDLE Answers 13

c. The default username is IoT_user with no password. Click the blue bar IoT_user in the middle of the screen to log in to the VM.

2.1.3.8 Lab - Create a Simple Game with Python IDLE Answers 14

Step 2: Navigate to the user Document directory

a. To access the command line interface, click Application on the menu bar and choose Terminal.

2.1.3.8 Lab - Create a Simple Game with Python IDLE Answers 15

b. Use the pwd command to display the current directory.

2.1.3.8 Lab - Create a Simple Game with Python IDLE Answers 16

c. Use the ls command to show the list of contents in the current directory. Use the ls command with the -l option to show detailed information about the contents.

2.1.3.8 Lab - Create a Simple Game with Python IDLE Answers 17

d. Use the cd Documents command to change the directory to the /home/IoT_user/Documents directory. Verify by using the command pwd.

2.1.3.8 Lab - Create a Simple Game with Python IDLE Answers 18

Part 2: Create a Simple Game with Python IDLE

In Part 2, you will create a simple game. The goal of the game is to find a number chosen by a player within 0 and 1024 using the bisection method.

Step 1: Launch IDLE.

a. Use the idle3 command to launch IDLE. By default, it starts in Python Shell, or interactive interpreter, window. You are already familiar with the interactive interpreter.

[IoT_user@stueverj-vm2 Documents]$ idle3

2.1.3.8 Lab - Create a Simple Game with Python IDLE Answers 19

b. Click File -> New File to open a new (Untitled) Python script.

2.1.3.8 Lab - Create a Simple Game with Python IDLE Answers 20

c. Type the codes in the script, and note that the codes are color coded with open and close parentheses matched.

2.1.3.8 Lab - Create a Simple Game with Python IDLE Answers 21

x=input("Enter an integer between 0 and 1024 --")
x=int(x)
a=0
b=1024
test=True
if x == 0:
	print("Your number is 0, thank you for playing.")
	test=False
else:
	if x == 1024:
		print("Your number is 1024, thank you for playing.")
		test=False

	while test == True:
		m=int((a+b)/2)
		if m == x:
			print("Your number is ", m,", thank you for playing")
			break
		else:
			if m < x:
				a = m
			else:
				b=m

d. Click File -> Save, and save the current script as 2.py in the current directory. Click the Save button.

e. Click Run -> Run Module (or press F5). The shell window will display the result.
2.1.3.8 Lab - Create a Simple Game with Python IDLE Answers 22

f. Troubleshoot if an error occurs during code syntax evaluation.

Reflection

How to catch if the player enters a number beyond the range of 0 to 1024?
The input should be verified to be within the correct range immediately after the player enters the number.

How to catch if the player enters a float number?
The input should be checked for an integer immediately after the player enters the number.

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x