CTS1133C-M03Prt01-NETLAB15.8-202210
pdf
keyboard_arrow_up
School
Florida State College at Jacksonville *
*We aren’t endorsed by this school
Course
2092
Subject
Computer Science
Date
Jun 24, 2024
Type
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.
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
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.
LAB 15:
Introduction to Scripting Labs 201
LAB
15
LAB FIGURE 15.9 Results of the second Python script
14. Save your python2
xxx
.py
file with a new filename: python3
xxx
.py
. For this script, you will add a while
loop so a user can repeat the process of entering values as often as desired.
Add the following lines to the top of the program:
# loop to re-enter a new value
choice = "y"
while choice == "y":
Next, indent each line of your previous code one time by pressing †
at the start of each line.
Add the following line at the end, also indented once:
choice = input("Enter another value? (y/n) ")
An abbreviated version of the code looks like this:
# loop to re-enter a new value
choice = "y"
while choice == "y":
# 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%"
...(remainder of code)
print("Your final cost is $ ", cost)
choice = input("Enter another value? (y/n) ")
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
202
Complete A+ Guide to IT Hardware and Software
Note that your entire program from the previous file must be between the new lines. The first new line of code is a comment. The next line declares a variable named choice
and sets its value to y
. The while
loop tests to see if choice
has the value y
. If it does, the loop is entered, and the program proceeds. After running once, you are prompted to enter y
or n
. Then control returns to the while
test. If you entered y
, the condition is true, and the program runs again. If the entry is anything other than y
, the condition is false, and the loop statements are skipped. In this case, the program ends.
15. Now save and run your program. If you enter 63
at the first prompt, y
at the second prompt, 245
at the third prompt, and n
at the last prompt, you should see the results shown in Lab Figure 15.10 in the Python Shell window.
LAB FIGURE 15.10 Results of the improved Python script
16. You may have noticed that the user can enter anything when prompted for a decision about whether to run the program again. If the user enters a lowercase y
, the program loops around again. But any other entry from the keyboard will stop the program. However, the prompt specifies entering either y or n
. This is done mainly for the user’s sake because it seems to make more sense to allow the user to say, in effect, yes or no in answer to the question. However, you can change this by changing this line:
choice = input("Enter another value? (y/n) ")
to the following (all on the same line):
choice = input("Enter another value? Type y to
continue or any other key to end. ")
Note: Because of space constraints in this text, the code shown above appears on two lines. But you must type the whole thing on a single line.
Add this change to your program and run it again. Be sure it works correctly before continuing.
Instructor initials: _____________
LAB 15:
Introduction to Scripting Labs 203
LAB
15
17. You may also have noticed that a user can type an uppercase Y
to continue, but if that happens, the program ends. This is because computers are case-sensitive. If you want to give the user the option to use uppercase or lowercase entries, you can use a logical operator.
Change this line:
while choice == "y":
to the following:
while choice == "y" or choice == "Y":
Make this change and test your program with both upper- and lowercase entries.
Instructor initials: _____________
On Your Own
Note: Most programming problems can be solved in a variety of ways. Your solution may not be exactly like your classmates’ solutions, but as long as the problem is solved, one method is not necessarily better than another. As you become more proficient with writing scripts, you may find more efficient ways to write code. 18. Your next task is to revise the code of your python3
xxx
.py
file to determine whether the purchase price for a 0% discount is both less than 50.00 and greater than 0. One solution is to add an elif
clause to check whether the cost entered is greater than 0 and revise the else
clause so it will dis-
play a message that any other amount is invalid. Updates to the results section will also be needed. Modify the code and save it as python4
xxx
.py
. Execute the program to test it.
Write your code here.
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
204
Complete A+ Guide to IT Hardware and Software
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
Instructor initials: _____________
19. Add tax to the final cost in the python4
xxx
.py program and save the new program as python5
xxx
.py
. Assume that the tax rate is 6.25%. The final output should include the discounted cost plus 6.25% tax. Run the program to verify your code. Hint: The code cost = cost + (.0625*cost)
can help here.
Write your code here.
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
Did the code run properly? [ Yes | No ]
Instructor initials: _____________
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
LAB 15:
Introduction to Scripting Labs 205
LAB
15
20. Create a new Python program, python6
xxx
.py
, to assign a letter grade to a student when a nu-
merical grade is input. The grades should be as follows:
90–100: A
80–89.99: B
70–79.99: C
60–69.99: D
Under 60: F
Write the code you used for this program.
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
Did the code run properly? [ Yes | No ]
21. Add a loop to your python6
xxx
.py
program that will allow the user to enter information for as many students as desired. Save the new program as python7
xxx
.py.
Test the program.
Write the modified program here.
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
206
Complete A+ Guide to IT Hardware and Software
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
Did the code run properly? [ Yes | No ]
Instructor initials: _____________
22. Add a greeting to your python7
xxx
.py
program to explain to the user what the program does. The greeting should appear only once. Consider the placement of this greeting within the program (before or inside the loop). Save the modified program as python8
xxx
.py
.
Write the modified program.
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
Did the code run properly? [ Yes | No ]
Instructor initials: _____________
LAB 15:
Introduction to Scripting Labs 207
LAB
15
23.
Create a new Python program, python9
xxx
.py
, that accepts input from the user and generates an
email address, as follows:
First name: Sam
Last name: Throckmorton
Welcome Sam!
Your new email address is Sam.Throckmorton@mycollege.edu
The email address should consist of the user’s first name, a dot, and the user’s last name, followed by
@mycollege.edu
.
Write the code you used to create the program.
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
Did the code run properly? [ Yes | No ]
Instructor initials: _____________
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
Related Documents
Recommended textbooks for you
![Text book image](https://www.bartleby.com/isbn_cover_images/9781305080195/9781305080195_smallCoverImage.gif)
Systems Architecture
Computer Science
ISBN:9781305080195
Author:Stephen D. Burd
Publisher:Cengage Learning
![Text book image](https://www.bartleby.com/isbn_cover_images/9781337671385/9781337671385_smallCoverImage.jpg)
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
![Text book image](https://www.bartleby.com/isbn_cover_images/9781305082168/9781305082168_smallCoverImage.gif)
Fundamentals of Information Systems
Computer Science
ISBN:9781305082168
Author:Ralph Stair, George Reynolds
Publisher:Cengage Learning
Np Ms Office 365/Excel 2016 I Ntermed
Computer Science
ISBN:9781337508841
Author:Carey
Publisher:Cengage
![Text book image](https://www.bartleby.com/isbn_cover_images/9781337097536/9781337097536_smallCoverImage.gif)
Fundamentals of Information Systems
Computer Science
ISBN:9781337097536
Author:Ralph Stair, George Reynolds
Publisher:Cengage Learning
![Text book image](https://www.bartleby.com/isbn_cover_images/9781305971776/9781305971776_smallCoverImage.gif)
Principles of Information Systems (MindTap Course...
Computer Science
ISBN:9781305971776
Author:Ralph Stair, George Reynolds
Publisher:Cengage Learning
Recommended textbooks for you
- Systems ArchitectureComputer ScienceISBN:9781305080195Author:Stephen D. BurdPublisher:Cengage LearningEBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENTFundamentals of Information SystemsComputer ScienceISBN:9781305082168Author:Ralph Stair, George ReynoldsPublisher:Cengage Learning
- Np Ms Office 365/Excel 2016 I NtermedComputer ScienceISBN:9781337508841Author:CareyPublisher:CengageFundamentals of Information SystemsComputer ScienceISBN:9781337097536Author:Ralph Stair, George ReynoldsPublisher:Cengage LearningPrinciples of Information Systems (MindTap Course...Computer ScienceISBN:9781305971776Author:Ralph Stair, George ReynoldsPublisher:Cengage Learning
![Text book image](https://www.bartleby.com/isbn_cover_images/9781305080195/9781305080195_smallCoverImage.gif)
Systems Architecture
Computer Science
ISBN:9781305080195
Author:Stephen D. Burd
Publisher:Cengage Learning
![Text book image](https://www.bartleby.com/isbn_cover_images/9781337671385/9781337671385_smallCoverImage.jpg)
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
![Text book image](https://www.bartleby.com/isbn_cover_images/9781305082168/9781305082168_smallCoverImage.gif)
Fundamentals of Information Systems
Computer Science
ISBN:9781305082168
Author:Ralph Stair, George Reynolds
Publisher:Cengage Learning
Np Ms Office 365/Excel 2016 I Ntermed
Computer Science
ISBN:9781337508841
Author:Carey
Publisher:Cengage
![Text book image](https://www.bartleby.com/isbn_cover_images/9781337097536/9781337097536_smallCoverImage.gif)
Fundamentals of Information Systems
Computer Science
ISBN:9781337097536
Author:Ralph Stair, George Reynolds
Publisher:Cengage Learning
![Text book image](https://www.bartleby.com/isbn_cover_images/9781305971776/9781305971776_smallCoverImage.gif)
Principles of Information Systems (MindTap Course...
Computer Science
ISBN:9781305971776
Author:Ralph Stair, George Reynolds
Publisher:Cengage Learning