0% found this document useful (0 votes)
11 views30 pages

07 Print and Input

The document outlines an online course by UNESCO UNITWIN, focusing on using print() and input() functions in programming. It includes examples, exercises, and explanations on how to handle user input and output in Python, including type conversion for numerical operations. Additionally, it provides practice problems and contact information for further inquiries.

Uploaded by

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

07 Print and Input

The document outlines an online course by UNESCO UNITWIN, focusing on using print() and input() functions in programming. It includes examples, exercises, and explanations on how to handle user input and output in Python, including type conversion for numerical operations. Additionally, it provides practice problems and contact information for further inquiries.

Uploaded by

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

UNESCO UNITWIN

Online Course
Supported by

Handong Global University


Ministry of Education, Korea

* All copyrights belong to UNESCO UNITWIN and it is prohibited to use these educational materials including the video and other
relevant materials for commercial and for-profit use.
Print and Input
07
Global Leadership School
Handong Global University
Learning Objectives
• Use print()
• Use input() with variables
• Practice Input/Output with exercise questions
Output to Screen
• print()
• Statement used to print (output)
• Used to print a string, numbers, and expressions
• Can print without print() statement in interactive
mode
Output to Screen, Example
>>> print(“John”)
John
>>> name = “John”
>>> print(“Hello,” name)
Hello John
>>> score = 78
>>> print(score)
78
>>> print(“name” , name, “score”, score)
name John score 78
>>> print( “abba” * 3)
abbaabbaabba
Output to Screen, Example
i=100
j=3
print(i+j)

i=i+100
print(“score is “, i)

twinkle = '*'
print(twinkle * 10)
Different examples
• Print expressions
• Use variables
Input from Screen
• Input()
• Used to receive input from users
• The input is read in as a string
• Need variable to store input data
• Example below uses the variable name “height”
Using Input and Output
• Input/print statements are always used
• Input and print statements are used together

• Print() may look simpler compared to input(),


• The inputted values must be saved to be available
within the program
• Variable is needed to save
• Create a variable name that matches the purpose
of the entered value and store.
• Later, access the value using the variable
When using input as a number
(1/3)
• BMI example from Week 2
• Receive height and weight
• This will cause an error
When using input as a number
(2/3)
• TypeError: when trying to calculate string like
a number
• Need to convert date type to data types allowed
for formula operation
When using input as a number
(3/3)
• Written as a script
## Calculate BMI

weight=input(“What is your weight in Kg: “)


weight=float(weight)

height=input(“What is your height in meters: “)


height=float(height)

bmi = weight / (height * height)


print(“Your BMI is “, bmi, “.”)
## Calculate BMI (short ver.)

weight=float(input(“What is your weight in Kg “))

height=float(input(“What is your weight in meters: “))

bmi = weight / height ** 2


print(“Your BMI is “, bmi, “.”)
Input from Screen, Example
>>> name = input('Enter your name ; ')
Enter your name ; Joseph
>>> print(name)
Joseph name = input('Enter your name ; ')
age = input('Enter your age ; ')
>>> age = input('Enter your age ; ')
Enter your age ; 17 print('My name is ', name)
print('Age =', age)
>>> print(age)
17
>>> print('My name is ', name, ', and', age, 'years old.‘ )
My name is Joseph , and 17 years old.
>>> print('My name is \"', name, '\“.')
My name is “Joseph”.
Exercise
• Receive Korean, English, and Math grades
from the user
• Calculate the average score of the three
subjects and print on the screen
Exercise 1, Code
## Calculate average

kor=input(“Your Korean grade? “)


kor=float(kor)

eng=input(“Your English grade? “)


eng=float(eng)

math=input(“Your math grade? “)


math=float(math)

avg = (kor + eng + math) / 3


print(“Average of the three subjects are “, avg, “.”)
Exercise 2
• Receive name from the user
• Print like following

>>> KKKKK
>>> IIIII
>>> MMMMM
Exercise 2, Code
## Split name and print

name=input(“Enter first 3 letters of your name; “)

print( name[0] * 5 )

print( name[1] * 5 )

print( name[2] * 5 )
Exercise 3
• Receive name, institution, and year of birth
from the user
• Print name and institution and calculate the
age in 2019
Exercise 3, Code and output
## Print name, institution, and age

name=input(”What is your name; ")

belong=input(”What is your institution; ")

birthyear=int(input(”What is your birthyear; "))

print(”Your name is", name)


print(“You are belong to “, belong)
print(”You are", 2019-birthyear, ”years old. Right?")
Exercise 4
• Input the price of the product
• Input the amount paid for the purchase
• Print out the change
Exercise 4, Code and output
cost=input(”Enter price of the product: ")

belong=input(”Enter the amount paid for the purchase: ")

rest=int(belong)-int(cost)

print(”The product cost is"+cost+”WON and the change is "


+str(rest)+”WON")
Lecture Summary
• Use print()
• Used with characters, numbers, and expressions
to print to screen
• Use input() with a variable
• Used to receive input from users
• Received input is stored as string
• Need variable to store the received input.
• Practice input and printing using exercise
questions
Practice Problem
• Which statement is wrong about the input
statement?
• It is a command used to get input from a user
• The data type of the input is a string or integer
• It needs to be saved to a variable to be used later
• When saved to a variable, it is always available
Practice Problem Answer
• Which statement is wrong about the input
statement?
• It is a command used to get input from a user
• The data type of the input is a string or integer
• It needs to be saved to a variable to be used later
• When saved to a variable, it is always available
Practice Problem
• Which of the following codes causes an error?
name = input('Enter your name ; ') age = input('Enter your age ; ')
Enter your name ; Joseph Enter your age ; 17
print(name * 2) print(age + 30)

weight=float(input(“Enter your weight in kg: “))

height=float(input(“Enter your height in meters: “))

bmi = weight / height ** 2


Practice Problem Answer
• Which of the following codes causes an error?
name = input('Enter your name ; ') age = input('Enter your age ; ')
Enter your name ; Joseph Enter your age ; 17
print(name * 2) print(age + 30)

weight=float(input(“Enter your weight in kg: “))

height=float(input(“Enter your height in meters: “))

bmi = weight / height ** 2


Thank you
07 Print and Input
Contact Info
[email protected]
https://ecampus.handong.edu/

* All copyrights belong to UNESCO UNITWIN and it is prohibited to use these educational
materials including the video and other relevant materials for commercial and for-profit use.
CREDITS: This presentation template was created by Slidesgo, including icons by Flaticon, and infographics & images by Freepik.

You might also like