Section 1.13 - IT 140_ Introduction to Scripting _ zyBooks
pdf
keyboard_arrow_up
School
Southern New Hampshire University *
*We aren’t endorsed by this school
Course
140 - X625
Subject
Mathematics
Date
Apr 3, 2024
Type
Pages
4
Uploaded by DeaconCaterpillar4154
Students:
Section 1.13 is a part of 1 assignment:
1-3 zyBooks Participation Activities
Includes:
PA
1.13 Variables and assignments
Remembering a value
Here's a variation on a common school child riddle.
CHALLENGE
ACTIVITY
1.13.1: People on bus.
For each step, keep track of the current number of people by typing in the num_people box (it's
editable).
553398.3976864.qx3zqy7
By the way, the real riddle's ending question is actually, "What is the bus driver's name?" The subject usually says, "How
should I know?" The riddler then says, "I started with YOU are driving a bus."
The box above serves the same purpose as a variable
in a program, introduced below.
Variables and assignments
In a program, a variable
is a named item, such as x or num_people, used to hold a value.
An assignment statement
assigns a variable with a value, such as x = 5. That statement means x is assigned with 5, and
x keeps that value during subsequent statements, until x is assigned again.
An assignment statement's left side must be a variable. The right side can be an expression, so a statement may be x = 5,
y = x, or z = x + 2. The 5, x, and x + 2 are each an expression that evaluates to a value.
PARTICIPATION
ACTIVITY
1.13.1: Variables and assignments.
= is not equals
In programming, = is an assignment of a left-side variable with a right-side value. = is
NOT equality as in mathematics. Thus, x = 5 is read as "x is assigned with 5," and not as
"x equals 5." When one sees x = 5, one might think of a value being put into a box.
PARTICIPATION
ACTIVITY
1.13.2: Valid assignment statements.
Indicate which assignment statements are valid.
1)
x = 1
Valid
Invalid
2)
x = y
Valid
Invalid
3)
x = y + 2
Valid
Invalid
4)
x + 1 = 3
Valid
Invalid
Start
You are driving a bus. The bus starts with 5 people.
num_people: 5
2
3
4
5
Check
Next
Feedback?
x = 5
x
y
y = x
z = x + 2
x = 3
z
x + y = 5
x * y = 6
Algebra
x = 2
y = 3
Programming
5
5
7
3
Captions
Feedback?
1
Start
2x speed
5)
x + y = y + x
Valid
Invalid
PARTICIPATION
ACTIVITY
1.13.3: Variables and assignment statements.
Given variables x, y, and z.
1)
x = 9
y = x + 1
What is y?
2)
x = 9
y = x + 1
What is x?
3)
x = 9
y = x + 1
x = 5
What is y?
PARTICIPATION
ACTIVITY
1.13.4: Trace the variable value.
Select the correct value for x, y, and z after the following statements execute.
Assignments with variable on left and right
Because in programming = means assignment, a variable may appear on both the left and right as in x = x + 1. If x was
originally 6, x is assigned with 6 + 1, or 7. The statement overwrites the original 6 in x.
Increasing a variable's value by 1, as in x = x + 1, is common and known as incrementing
the variable.
PARTICIPATION
ACTIVITY
1.13.5: A variable may appear on the left and right of an assignment
statement.
PARTICIPATION
ACTIVITY
1.13.6: Variable on both sides.
Feedback?
Check
Show answer
Check
Show answer
Check
Show answer
Feedback?
Start
int x = 2;
int y = 7;
int z = 8;
x = 8;
y = 5;
z = 9;
x = 1;
x is
2
8
1
y is
0
7
5
z is
3
8
9
2
3
4
Check
Next
Feedback?
Put "A person with measles may cause " to output
x = 1
x = x * 20
x = x * 20
Put x to output
Put "people to be infected in two weeks." to output
x
A person with measles may cause
Put newline to output
400
people to be infected in two weeks.
1 20
400
1. A variable may appear on both sides of an assignment statement. After x = 1, then x = x * 20
assigns x with 1 * 20 or 20, overwriting x's previous 1.
2. Another x = x * 20 assigns x with 20 * 20 or 400, which overwrites x's previous 20.
3. Only the latest value is held in x.
Captions
Feedback?
1
Start
2x speed
Indicate the value of x after the statements execute.
1)
x = 5
x = x + 7
2)
x = 2
y = 3
x = x * y
x = x * y
3)
y = 30
x = y + 2
x = x + 1
4)
Complete this statement to
increment y: y = _____
CHALLENGE
ACTIVITY
1.13.2: Assigning a sum.
Write a statement that assigns total_coins with the sum of nickel_count and dime_count.
Sample output for 100 nickels and 200 dimes is:
300
Learn how our autograder works
553398.3976864.qx3zqy7
CHALLENGE
ACTIVITY
1.13.3: Multiplying the current value of a variable.
Write a statement that assigns cell_count with cell_count multiplied by 10. * performs
multiplication. If the input is 10, the output should be:
100
Learn how our autograder works
553398.3976864.qx3zqy7
Check
Show answer
Check
Show answer
Check
Show answer
Check
Show answer
Feedback?
total_coins = 0
nickel_count = int
(
input
())
dime_count = int
(
input
())
''' Your solution goes here '''
print
(
total_coins
)
Run
View your last submission
Feedback?
cell_count = int
(
input
())
''' Your solution goes here '''
print
(
cell_count
)
Run
View your last submission
1
2
3
4
5
6
7
8
1
2
3
4
5
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
Activity summary for assignment: 1-3 zyBooks Participation Activities
98 %
98 % submitted to desire2learn
How was this
section?
|
Feedback?
Provide section feedback
Completion details