tutorial3

docx

School

Algonquin College *

*We aren’t endorsed by this school

Course

COMP1012

Subject

Computer Science

Date

Feb 20, 2024

Type

docx

Pages

5

Uploaded by jochonuwe

Report
Tutorial 3 Please note: these tutorials are designed to provide practice on various concepts from class - think of the tutorial worksheets as a database of exercises. You are not expected to finish everything in a given tutorial . We do encourage you to continue working on the worksheet outside of tutorial time, because the more practice one gets with programming, the easier it becomes. Please remember to upload your tutorial worksheet before you leave (it does not need to be completed for your to upload – you must upload to get full credit for attending). Part 1: Code tracing with while loops 1a) Consider the program below. Trace the program. What is printed? Answer: 4 Check your answer by running the program. 1b) Consider the program below. Trace the program. What is printed? Answer: 1 Check your answer by running the program. What happened in this program – did the code in the loop run? Describe. It did not run because counter is less than 3 so it printed it without breaking the loop 1c) Consider the program below.
Trace the program. What is printed? Answer: 3 Check your answer by running the program. Note: the answer for this program is different from the program in 1a and 1b. Reflect on why. 1d) Consider the program below. Trace the program. What is printed? Answer: 3 3, 5 8 Check your answer by running the program. Part 2: Fill in the blank For the program below, what code is needed in the blank (yellow box) to make the program produce the output shown on the right? Program Output counter 4 Answer: if counter > 5 Part 3: Writing code with while loops Write a program that assigns a “secret” password to a variable called password , i.e., line 1 of the program looks like this:
Password = “test123” User_input = input(“Enter the password: “) If user_input == password: Print(“Password correct, welcome to the system”) Else: Print(“ Incorrect password”) Then add code to make the program keep asking the user for a password until the user enters the correct password (i.e., test123). Here is sample run of the program, where the user spaced out and entered the same incorrect password 3 times, and then entered another incorrect password, and then had a flash of recollection and entered the correct password: Hints: - note: the program stopped asking for a password as soon as test123 was produced - use a while loop in your program - you can either use a while True (and then remember to include a break inside it), or you can use a while < condition > where condition is the condition that makes the loop stop o inside the while loop you will need to ask the user for a password, assign to a variable (called something like userPassword ) and check if that userPassword is equal to the actuall password (stored in the variable called password ) Where do I put my code? Either upload your .py file to Brightspace or paste code here: Appendix : Additional exercises: More loop-tracing questions are in this google doc:
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
tinyurl.com/ybsazfqv More fill in the blank questions: For the programs below, what code is needed in the blank (yellow box) to make the program produce the output shown on the right? A1 Program Output 5 3 X > 0 A2 Program Output 3 3 X > 3 A3 Program Output in blue below assuming user entered: 5 8 2 -1 4 15 Y = y + val A4 Program:
Here is the run of the program with 3 user inputs (shown in black): If not char break A5 Program Output from the program: Counter > 0: A6 Program Here is the run of the program with three user inputs (shown in black): Tot += val