CTS1133C-M03Prt01-NETLAB15

.pdf

School

Florida State College at Jacksonville *

*We aren’t endorsed by this school

Course

2092

Subject

Computer Science

Date

Jun 24, 2024

Type

pdf

Pages

12

Uploaded by ChefFreedom10543

196 Complete A+ Guide to IT Hardware and Software Lab 15.8 Introduction to Python Scripting Objective: To create and run Python scripts using IDLE Parts: Windows computer with access to the internet and rights to download and install free software Procedure: Complete the following steps and answer the accompanying questions 1. Power on the computer and log on. Open a web browser. 2. In the browser, go to https://www.python.org. Click Downloads > Download Python 3.10.4 . Save the file python-3.10.4-amd64.exe to a place on the computer where you can find it.
LAB 15: Introduction to Scripting Labs 197 LAB 15 Note: At the time of this writing, Python 3.10.4 was the latest version. If that has changed, you can download a later version. However, to do this lab, you must have a version of Python 3 installed ( not Python 2!). The executable file that you downloaded installs Python on your computer. This will allow you to create programs in the Python language, using the integrated development environment (IDE) named IDLE. (Note: IDE is a generic term for a program used by a language to write programs, and IDLE is the name of the IDE used here to write Python programs.) 3. Run the executable file and follow the prompts. Accept all the default options. If your installation is successful, you should see a verification similar to the one shown in Lab Figure 15.4. LAB FIGURE 15.4 Successful installation of Python 4. From Windows Start, expand the Python 3.10 folder, if necessary, and click on IDLE (Python 3.10 64-bit), as shown in Lab Figure 15.5. LAB FIGURE 15.5 IDLE (Python 3.10 64-bit) in the Start menu
198 Complete A+ Guide to IT Hardware and Software 5. You should see the IDLE Shell 3.10.4 window. This is also called the Python Shell. Make it active by clicking anywhere in its blank area. The cursor should be blinking to the right of the three chevrons: >>> . Press ® two times, and you should see two more rows of chevrons. This means that Py- thon is working and waiting for you to do something. 6. In the Python Shell window, type the following: print("I got it to work!") Press ® , and you should see a screen like the one shown in Lab Figure 15.6. LAB FIGURE 15.6 Working in the Python IDLE shell 7. To prepare for your work, create a folder called PythonTest . Make sure you know where this folder is located. 8. The Python Shell window is where you see a script run. To create a script, you need to work in the Python editor. From the menu within the Python Shell window, select File > New File to open the Python editor. Type the following into the editor: # display a greeting and generate a password # get input from the user first_name = input("First name: ") last_name = input("Last name: ") id_number = input("Enter your school ID number: ") # create strings name = first_name + " " + last_name temp_password = first_name + "*" + id_number # display the results print("Welcome " + name + "!") print("Your temporary password is: " + temp_password) Be sure to type this code exactly as shown. In Python, indents, spaces, and line spacing are very important: > In this program, any line that begins with a hash symbol ( # ) is a comment and will be ignored by the computer when the program runs. > The following are the variable names in this script: first_name , last_name , id_number , name , and temp_password . The value on the right side of the equals sign is the value stored in the variable on the left side.
LAB 15: Introduction to Scripting Labs 199 LAB 15 > In the line first_name = input("First name: ") , the variable is first_name . The word input is a Python function that tells the computer to put a prompt on the screen. The text of the prompt is whatever is enclosed in quotes inside the parentheses. In this case, the text First name: is displayed. When the user enters their name, that value is stored in the variable first_name . > The name variable joins the values of two variables and adds a space between those values because of the space within quotes ( " " ). > The print() command tells the computer to display something on the screen. It can be text, the value of a variable, or a combination of text and variables. 9. Save your program with the filename python1 xxx .py (where xxx is your initials) inside your PythonTest folder by going to the File tab and selecting Save As . 10. Now you can run your program. Go to the Run tab at the top of the Python editor screen, as shown in Lab Figure 15.7, and click on Run Module . The program should open in the Python shell and prompt you for your first name. LAB FIGURE 15.7 The Run tab in the Python IDLE editor If you get an error, go back to the editor and compare your code with the code given above. Fix any errors you find, save the changes, and try again. 11. Nothing happens until you enter something. Enter Jackie for the first name and enter Cho for the last name. Enter 123456 for the school ID number. You should see the results in the Python shell, as shown in Lab Figure 15.8. LAB FIGURE 15.8 Results of the first Python script Did the script work? [ Yes | No ] If the script did not work, troubleshoot and edit your code as necessary until it does. Instructor initials: _____________
200 Complete A+ Guide to IT Hardware and Software 12. Create a new Python program. Name this new file python2 xxx .py (where xxx is your initials). This new program will use a decision structure to decide whether a user is eligible for a 20%, 10%, or 0% discount on a purchase. In this program, the user will enter the cost of a purchase. If the purchase value is over $100.00, the discount is 20%. If the purchase value is $50.00 to $99.99, the discount is 10%. Anything under $50.00 is not eligible for a discount. The code for this program is as follows: # prompt user for cost of purchase cost = float(input("How much is your purchase? $ ")) # check for discount amount if cost >= 100: discount = cost * .20 percent = "20%" elif cost >= 50: discount = cost * .10 percent = "10%" else: discount = 0 percent = "0%" # display the results cost = cost - discount print("You have received a " , percent, " discount") print("Your final cost is $ ", cost) 13. Run the program at least three times to check that all the discount options (0%, 10%, or 20%) work. You can enter any numbers you want, but here are some suggestions for input: 105 87 50 34 If you enter 87 at the prompt, you should see the results shown in Lab Figure 15.9. You might notice that there are some things that could be improved in this program: > Try entering a negative number. The program will still run, but it makes no sense to have a negative value as an input. You can fix this later as an On Your Own exercise. > The output shows a dollar amount but has only one decimal place where you would expect two. There are ways to deal with this issue, but they require more advanced coding, so for now, this is fine. > Each time you want to enter a new value, you must run the program again. By using a loop, you can allow the user to enter as many values as desired. You will add this functionality in the next step.
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help