PYTHON
Is attach below:
CSCI333 Assignment 04
Control Structure II – Repetition
1. (30 points, each 5 points) Multiple choice or True/False questions:
1) (5 points) range (5) is:
a) 0, 1,
2
, 3, 4, 5
b) 1, 2, 3, 4, 5
c) 0, 1, 2, 3, 4
d) 1, 2, 3,4
Answer:
2) (5 points) range (6, 0, -2) is
a) 6, 4, 2
b) 6, 4, 2, 0
c) 6, 4, 2, 0, -2
d) -6, -4, -2, 0, -2
Answer:
3) (5 points) Executing a break statement in while or for loop exits the loop immediately
a) True
b) False
Answer:
4) (5 points) Executing a continue statement in while or for loop skips the remainder of the loop’s suite, and go to the top of the loop and continue with the next iteration (if any)
a) True
b) False
Answer:
5) (5 points) Executing an else statement in a for loop specifies a block of code to be executed when the loop is finished:
for i in range(m):
print(i)
else:
print(“Done”)
a) True
b) False
Answer:
6) (5 points)
The following loop is executed times:
a) a
int x = a
while x <= 2a :
x = x + 1
b) a + 1
c) a – 1
d) a + 2
Answer:
2. (30 points, each 5 points) Hand-trace the following code. What is the output or what error/problem do you observe?
1) (5 points)
n=1 while n < 7: print (n) if n==3: break n += 1 |
Output: |
2) (5 points)
n=1
while n != 50: print(n) n=n + 10 print (“Completed”) |
Output: |
3) (5 points)
for n in range (0, 9, 3): if n == 3: continue print (n) |
||
Output: |
4) (5 points)
for x in range (2): for y in range (3): print (x, y) else: print (“Finally done!”) |
5) (5 points)
for x in range(3): for y in range(x+2): print(“*”, end =””) print() |
Output: |
6) (5 points)
for i in range(3) : for j in range(5) : if j % 2 == 1 : print(“*”, end=””) else : print(“−”, end=””) print() |
3. (15 points) Write a program to compute and output the sum of all even numbers between 2 and 100 (including 2 and 100)
· (10 points) Write your program here, or copy/paste a screenshot of your Program:
· (3 points) Screenshot of a Program Run:
· (2 points) Save the program as “sumEvenNum.py”. Upload the .py file as part of your submission.
4. (15 points) Write a program to take a positive number n from user input, compute and output the sum of all odd digits of n. (For example, if n is 32677, the sum would be 3+7+7 = 17)
For example, a program run:
· (10 points) Write the program here, or copy/paste the screenshot of your program
· (3 points) Screenshot of a Program Run:
· (2 points) Save the program as “sumOddDigits.py”. Upload the .py file as part of your submission.
5. (15 Points) Write a program to get a first integer input from the user, use while to get the next input and determine if the current input integer is the same with the previous input. If adjacent values are the same, output a message stating they are duplicate inputs. Process data until the sentinel “-1” is entered.
For example: A program run:
· (10 points) Write the program here, or copy/paste the screenshot of your program
· (3 points) Screenshot of a Program Run:
· (2 points) Save the program as “checkAdj.py”. Upload the .py file as part of your submission.
What to submit: a) One word file ( x) named ”CSCI333-Assign04-YourFirstLastName ”, including answers to all questions and the python source code in text or screenshots of all programs. b) All the Python source files (.py) of the programming questions c) Create a folder named as “CSCI333-Assign04-YourFirstLastName” and put the word file and Python source files into one folder Submission d) Zip (compress) the folder and submit the zipped file in myLeo Note: Partial credit will be given so do what you can and make sure you show your work for each question. No credit be given if no work is shown. For example, as long as you submit the screenshot of the programming result and the .py file, you will get the corresponding points. |
Submission example: CSCI333-Assign04-YanLi.zip CSCI333-Assign04-YanLi sumEvenNum.py sumOddDigits.py checkAdj.py |
2