You are on page 1of 10

Lecture 2

Part 2 :
Input and if statements

ITE3711 - Programming Concept and Applications


Lecture 2 – Part 2

How to get user’s input in the Python program?

Python allows for user input and gets data from the console
• Python 3.6 using input() method
• Python 2.7 using raw_input() method

Python stops executing the program when the program comes to the input function, it will continue after the user iput

2
Lecture 2 – Part 2

How to get user’s input in the Python program?

• Python 3.6 using input() method input() method is used in this module

The console will display this message on the screen


number1 = input("Enter the first number:") before the input
print("First number: " + str(number1))
The inputted value will be stored in number1 in
this example

• Python 2.7 using raw_input() method

number1 = raw_input("Enter the first number:")


print("First number: " + str(number1))

3
Lecture 2 – Part 2

input() function – method 1

number1 = input("Enter the first number:")


print("First number: " + str(number1))
Output:

1
User input the value

The program received the user input

3
4
Lecture 2 – Part 2

input() function – method 2

print("Enter the first number:")


number1 = input()
print("First number: " + str(number1))
Output:

1 User input the value

2
The program received the user input

3 5
Lecture 2 – Part 2

input() function – error

For calculation, it is necessary to specify the data


type: int() or float()
number1 = input("Enter the first number:")
sum = number1 + 5
print("First number + 5 = " + str(sum))
Output:
sum = number1 + 5
~~~~~~~~^~~
TypeError: can only concatenate str (not "int") to str

For calculation, it is necessary to specify the data


To fix this error: type: int() or float()
number1 = int(input("Enter the first number:"))
sum = number1 + 5
print("First number + 5 = " + str(sum)) 6
Lecture 2 – Part 2

Example of input() function

number1 = int(input("Enter the first number:"))


number2 = int(input("Enter the second number:"))
sum = number1 + number2
print("Sum = " + str(sum))
Output:
User input the first number
1
User input the second number

2 The program received the user input, and


return the sum of two numbers

3 7
Lecture 2 – Part 2

Example of input() function

testMarks = int(input("Enter the Test marks: "))


workshopsMarks = int(input("Enter the Workshops marks: "))
projectMarks = int(input("Enter the Project marks: "))
CA = testMarks*0.2 + workshopsMarks*0.15 + projectMarks*0.15
print("Your CA marks is " + str(CA))
Output:
Enter the Test marks: 85
Enter the Workshops marks: 80 User input the
Enter the Project marks: 95 • Test marks
Your CA marks is 43.25 • Workshops marks
• Project marks

The program received the user inputs, and


return the calculated CA marks 8
Lecture 2 – Part 1

Combine conditional statements


module_marks = int(input("Enter the module marks:")) Output:

if module_marks >= 75:


grade = "A"
elif module_marks >= 65:
grade = "B" Output:
elif module_marks >= 55:
grade = "C"
elif module_marks >= 40:
grade = "D"
else:
grade = "fail"

print("Grade: " + grade)


9
Lecture 2 – Part 1

Combine conditional statements


weight = float(input("Weight(kg): ")) Output:
height = float(input("Height(m): "))
bmi = weight / (height * height)

print ("BMI: " + str(round(bmi,2)))

if bmi > 40:


category = "Obese Class III"
elif bmi > 35 and bmi <= 40:
category = "Obese Class II"
elif bmi > 30 and bmi <= 35: Output:
category = "Obese Class I"
elif bmi > 25 and bmi <= 30:
category = "Overweight"
elif bmi > 18.5 and bmi <= 25:
category = "Normal"
elif bmi > 17 and bmi <= 18.5:
category = "Mild Thinness"
elif bmi > 16 and bmi <= 17:
category = "Moderate Thinness"
elif bmi < 16:
category = "Severe Thinness" 10

print("Category: " + category)

You might also like