0% found this document useful (0 votes)
65 views

Lab - 01 - CS171

Here are the responses to the critical thinking questions: 1. When the Python program is executed, it prints "What is your name?" on the screen as a prompt for the user to enter their name. 2. a) When the line of code is executed, the prompt "What is your name?" appears on the screen for the user to enter their input. b) The data entered by the user gets stored in the variable "name". 3. a) There is a SyntaxError because a variable name cannot contain a question mark. b) There is a SyntaxError because a variable name cannot contain spaces. c) There is a SyntaxError because a variable name cannot start with a number. d) The

Uploaded by

Sai Musunuri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views

Lab - 01 - CS171

Here are the responses to the critical thinking questions: 1. When the Python program is executed, it prints "What is your name?" on the screen as a prompt for the user to enter their name. 2. a) When the line of code is executed, the prompt "What is your name?" appears on the screen for the user to enter their input. b) The data entered by the user gets stored in the variable "name". 3. a) There is a SyntaxError because a variable name cannot contain a question mark. b) There is a SyntaxError because a variable name cannot contain spaces. c) There is a SyntaxError because a variable name cannot start with a number. d) The

Uploaded by

Sai Musunuri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

CS 171 Computer Programming I

Lab 1
Content by Professor Lisa Olivieri of Chestnut Hill College
Modified for Drexel by Professors Mark Boady and Adelaida Medlock

Detailed instructions to the lab assignment are found in the following pages.

 Complete all the exercises and type your answers in the space provided.
 For the last question of this lab you must submit a screenshot your source code (.py
file) running in Thonny

What to submit:

 Lab sheet in PDF to Gradescope


 Questions must be tagged in Gradescope
 Your lab partner must be added to the submission
 Only one submission per lab group

Submission must be done via Gradescope

Student Name(s): Sai Musunuri, Varun Premanand

User ID(s) (abc123): sm4727, vp483

Possible Points: 80

Your score out of 80:

Lab Grade on 100% scale:


Python Activity 1: Introduction to Python

Learning Objectives
Students will be able to:
Content:
 Explain how to display data in Python
 Explain how to create a comment in Python
 Determine the difference between a string literal and a number
 Explain how to input data using Python
 Explain the meaning and purpose of a variable
 Determine if a variable name is valid
 Explain concatenation and the use of “+”
Process:
 Create print statements in Python
 Create Python code that displays results to calculated addition facts
 Discuss problems and programs with all group members
 Create input statements in Python
 Create Python code that prompts the user for data and stores it in a variable
 Create valid and good variable names

Prior Knowledge
 Be able to input and execute Python code using the Python IDE

Critical Thinking Questions (30 points):

1. (2pts) Enter the Python program shown above. What does it do?

When you use print(“Go!”) in the python program, it prints out the string in one line of text. In this
case the string literal is Go!

FYI: A "string literal" is a sequence of characters surrounded by double quotation marks (" ").
2. (6pts) Type and execute following code. What output is produced? Indicate if there is a problem.

a. print(“Hello, my name is Pat!”)

The string literal, Hello my name is Pat!, is printed on one line

b. print(Hello, my name is Pat)


When running this code, there is a syntax error because there are
no commas around the alleged string, therefore the string is
undefined.
c. print(“Hello.\nMy name is Pat”)

The string gets printed on two different lines. The top line is
“Hello.” and “My name is Pat” is printed on the line underneath
it.

3. (2pts) What caused the different output format for samples “a” and “c” in question 2?

There are different outputs because \n in question c acts as a new line character. It is used to indicate
the end of a line of text. Therefore, the string gets printed on two lines instead of one.

4. (8pts) What do you think the following Python statements output? Enter the statements in the
interactive mode of the Python interpreter to verify your answers.
a. print(2+5)

When entering this line of code, python prints out the number
“7” which is the simple sum of the 2+5
b. print(2*5)

When entering this line of code, python prints out the number
10, which is the answer to 2*5
c. print(“2+5”)

When you enter this code, it takes “2+5” as a string literal


instead of a number, therefore it prints out “2+5”.

d. print(“Age:”,20)

This code, when entered prints out “Age: 20”.

5. (8pts) Examine the output for each statement in question 4.

a. What is the difference in the output for the statements in “a” and “c” of question 4?

The difference is that in Part a, the sum gets printed out which is 10 and in Part b the entire
string gets printed out, “2+5”
b. What caused the difference?

The difference is caused by the addition of the quotations, the commas indicate that the
whatever is inside them is a string and not an integer.

c. Which statements contain a string literal?

Part C and Part D have string literals due to the addition of quotations inside the parenthesis
of the print function.

d. What does the comma (,) do in the print statement in part “d” of question 4? How does it
affect the spacing of the output?

The comma acts as a space within the print function and spaces out the string from the
integer.

6. (2pts) Examine the following code and its output. What do the first three lines of the program do?

Output

7. (2pts) In the program from question 6, what would happen if you placed a “#” in front of

If you place a hashtag in front of code, it becomes a comment that is not executed. In other words the
hashtag tells the interpreter to ignore the rest of the code on that line.

Application Questions: Use the Python program mode to design and check your work (4 points)

1. (2 points) Create a Python program containing three statements to print the following output. Copy
your program statements in the space below.

print("Congratulations!")
print("You just created")
print("your first Python program")

2. (2pts) Create a Python program containing two statements that prints the output
to the right. Have the program calculate the answers to the two arithmetic
problems. Copy your program statements in the space below.

print("34 + 123","=",34+123)
print("56 * 97", "=", 56*97)

Critical Thinking Questions (36 points):

1. (2pts) Enter and execute the Python program. What is printed on the screen when the Python program
is executed?

After typing the first line of code on the python program, it prints on the screen What is
your name? With implementing the name function, it is required for the user to input a
name into the Shell response. For example, if I respond to the question, “What is your
name?” with the response Bob, it will give a response under the line with “Your name is
Bob”

FYI: input() and print() are functions in Python.

2. (4pts) Examine the first line of Python program: name = input(“What is your name? ”)

a. What appears on the screen when this line of code is executed?

When the line of code is executed , the function is printed and asks for an input to
the question

FYI: The words that appear on the screen and tell the user what to enter are known as a prompt.

FYI: name = input(“What is your name? ”)


b. The word name in the Python code is a variable – a name given to a memory location used to
store data.
What happens to the data the user entered?
Once the data the user has entered, it will be printed on the next line with the
response the user had inputted on the first line

3. (4pts) Explain the errors that occur when you execute each of the following lines of Python code.

a. name? = input(“What is your name?”)

From executing this line of code, there is a SyntaxError in the variable of the
code due to them using a question mark with the variable name
b. your name = input(“What is your name?”)

From executing this line of code, there is an SyntaxError in the variable as there
should be no space between “your name.” The code should either be indicated
as “your_name” or “yourname”

c. 1st_name = input(“What is your name?”)

From executing this line of code, there is a SyntaxError in the variable as there is
an invalid decimal literal in the beginning of the variable.

d. from = input(“Where were you born?”)

From executing this line of code, there seems to be no SyntaxError in the code
indicating from the response given in the Shell.

4. (2pts) Examine the errors that occurred when executing the lines of code in question 3. Then
examine the following lines of valid code.

name2 = input(“What is your name?”)


your_name = input(“What is your name?”)
yourName = input(“What is your name?”)

List the rules that you need to follow to create a valid variable name.

1.     A variable name must start with a letter or the underscore character.
2.     A variable name can’t start with any numerical values, spaces, or any other special
characters.

5. (8pts) Are the following variable names valid? Are they good names? Why or why not?
Variable name Comments about variable name
The variable name is indeed valid. I believe this would be a
price good fitting name as the variable name is short, concise,
and easy to understand
The variable name is indeed valid. However, I do not believe
this is a good fitting name as the variable is too long,
costoffirstitem unconcise, hard to read. It should be written out in the
proper way by using the Camel case therefore stating
costOfFirstItem
The variable name is indeed valid.  However, I do not
Ic believe this is a good fitting name as the variable has no
meaning to any scenario given
The variable name is indeed valid. I believe this would be a
firstName good fitting name as the variable name is short, concise,
and easy to understand. It also efficiently uses the Camel
Case when stating this variable.
6. (2pts) Execute the following lines of code. Is the output what you would expect? Why or why not?

After executing the following lines of code, it is the output we expected as the variable
name is spelled differently on the first line than what is provided in the second line
after the print code therefore creating an error in the code.

7. (10pts) Use the following set of Python statements to answer the questions below.

a. State the output for each of line of code.

b. How are the first two print statements different? Does the difference affect the output?

c. Notice that some statements include a comma (,) between the two literals being printed and some
statements use a “+”. Do they produce the same output?
e. Explain the purpose of the comma.

e. Why does the last print statement crash the program? What would you do to correct it?

FYI: “+” concatenates two strings. The strings can be string literals or a variable containing string literals.
8. (2pts) State what is displayed on the screen when the following program is executed:

It prints three questions and when you answer each one, there is a final statement that shows your
name, student ID number, and course number.

Enter your name: Sai M


Enter your student ID number: 14474980
Enter your course number: 001
Sai M 's ID is 14474980
and is enrolled in 001

9. (2pts) What caused the output in the print statement in question 8 to be printed on more than one line?

The \n is what allowed the statement to be printed into multiple lines.

Application Questions:
Use the Python Interpreter to enter your code and check your work (10 points)

1. (2pts) State a good variable name for an employee’s ID number:

employer_id = (input ())


print(“Employer’s ID is: “, employer_id)
2. (2pts) Write a line of Python code that prompts the user for the name of their favorite ice cream
and stores it in a valid variable name.

IceCream = input("What is your favorite ice cream: ")


name = input("What is your name: ")

print(name + "'s favorite ice cream is " + IceCream)

3. (6pts) Crazy Sentence Program. Create a program that prompts the user for the name of an
animal, a color, the name of a vehicle, and the name of a city. Then print a sentence that contains
the user input in the following order: color, animal, vehicle, city. Include the additional words in
the sample output as part of your output. Example: Assume the user enters the words: tiger,
green, motorcycle, and Wildwood. The output would be: The green tiger drove the motorcycle
to Wildwood.

Once your program is complete, take a screenshot of the code and output of the program. You
may need multiple screenshots to capture all your code. Paste the screenshot below.
Your code must include comments with the names of all group members and the date the code
was written.

You might also like