Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

BANA3020 Lab2

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Lab Practice 2: Variables and Operators

BANA3020 Introduction to Programming with Python Fall 2023 – Week 02

Lab Practice Submission Instructions:


• This is an individual lab practice and will typically be assigned in the laboratory (computer lab). You can
use your personal computer but all quizzes and practical exams will be performed with a lab computer.
• Your program should work correctly on all inputs. If there are any specifications about how the program
should be written (or how the output should appear), those specifications should be followed.
• Your code and functions/modules should be appropriately commented. However, try to avoid making
your code overly busy (e.g., include a comment on every line).
• Variables and functions should have meaningful names, and code should be organized into function-
s/methods where appropriate.
• Academic honesty is required in all work you submit to be graded. You should NOT copy or share your
code with other students to avoid plagiarism issues.
• Use the template provided to prepare your solutions.
• You should upload your .py file(s) to the Canvas before the end of the day.
• Submit separate .py file for each Lab problem with the following naming format: Lab2_Q1.py. Note: If
you are working on Jupyter Notebook, you need to download/convert it to Python .py file for submission.
• Late submission of lab practice without an approved extension will incur the following penalties:

(a) No submission by the deadline will incur 0.25 point deduction for each problem (most of the
problems are due at the end of the lab session).

(b) The instructor will deduct an additional 0.25 point per problem for each day past the deadline.

(c) The penalty will be deducted until the maximum possible score for the lab practice reaches zero
(0%) unless otherwise specified by the instructor.

Lab Practice 2 Page 1


IO (input, output) in Python
A computer program often requires interactions with the user in the form of inputs and outputs (IO). A simple
form of output that you may be familiar with is the print function.

1 >>> print ( " Hello world ! " )


2 Hello world !

Now we can output information to the user, but how can we ask the for their input? Here is where you can
use the aptly named input function. Which prompts the user for a string input that we can use for the rest
of our program.
The syntax for the input function is: input("Your prompt here"), and will give whatever the user decides
to enter as a string.
In the examples for the rest of the semester, the text in teal represents what the user inputs, and the text
in violet represents our code to be executed by the Python interpreter. In the following example, the user
enters BANA3020. Which we can use to print a corresponding welcome message.

1 >>> class_name = input ( " Enter your class name : " )


2 Enter your class name : BANA3020
3 >>> print ( " Welcome to " + class_name + " ! " )
4 Welcome to BANA3020 !

Note that the return type of input() is a string. Recall that we have different data types in Python for
numbers and text (int for whole numbers, float for real numbers, str for text strings). Therefore, when
we prompt the user to enter a numerical value, we have to convert its type accordingly to be able to use it as
a number. Otherwise, you will get unexpected behaviour (often referred to as a bug)!

1 >>> number = input ( " Enter your number : " )


2 Enter your number : 2
3 >>> print ( number + number )
4 22
5

6 >>> new_number = int ( input ( " Enter your number : " ) )


7 Enter your number : 2
8 >>> print ( new_number + new_number )
9 4

You can use int(), float(), str(), and more to convert a value into a data type that you would like it to
be. Be careful, not all conversion makes sense! e.g. What would int("Hello!") gives?

Lab Practice 2 Page 2


IMPORTANT NOTE:
Your program’s output and behaviour should match exactly what is shown the the Example test case sections
of each question. In later weeks, we will use an automated scoring platform to evaluate your code, which does
not allow for human errors such as missing a comma, or spelling errors. Always make sure to double check
your print and input statements!

Problem 1 - Assignment Operators


Write a program that asks for the user’s name, the user’s age, and print out the information.

Example test case


1 >>> python Lab2_Q1 . py
2 Enter your name : Nguyen Van An
3 Enter your age : 20
4 Hello , this is your information :
5 Your name is : Nguyen Van An
6 Your age is : 20

Problem 2 – Average exam score


Write a program to ask the number of students in a class. Request the user to enter the total score of all
students and then output the average exam score of the class.

Example test case


1 >>> python Lab2_Q2 . py
2 Enter number of students : 25
3 Enter total exam score : 2185
4 Average exam score is : 87.4

Problem 3 – Numerical inputs


Prompt the user for two inputs and then output their sum and difference.

Example test case


1 >>> python Lab2_Q3 . py
2 Enter an integer : 15
3 Enter a float : 1.5
4 Total is 16.5 and difference is -13.5

Lab Practice 2 Page 3


Problem 4 – Time expression
Write a program to ask for a positive integer that represents the duration of an event (in seconds). Convert
the duration in seconds to hours, minutes, seconds. For example: 3891 seconds is 1 hour, 4 minutes, and 51
seconds.

Example test case


1 >>> python Lab2_Q4 . py
2 Duration in seconds : 3891
3 Hours : 1
4 Minutes : 4
5 Seconds : 51

Lab Practice 2 Page 4

You might also like