1. Trang chủ
  2. » Giáo án - Bài giảng

034 if elif else statements tủ tài liệu training

3 47 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 3
Dung lượng 380,77 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

if-elif-else statementsIn previous lesson, we have learned if-else statements.. In this lesson, we are going to see if-elif-else statements.. if-elif-else blocks Sometimes a situation ar

Trang 1

if-elif-else statements

In previous lesson, we have learned if-else statements In this lesson, we are going to see if-elif-else statements

if-elif-else blocks

Sometimes a situation arises when there are several conditions

To handle this situation Python allows us adding any number of elif clause after an if and before an else clause

Here is the syntax of if-elif-else statement:

if expression1:

do something

elif expression2: do something else

elif expression3: do something else

else: do another thing

In our programs, we can create as many elif blocks as we need also it's not mandatory to write else

In if - elif - else statements, which condition is satisfied the program executes that condition block and the if block is ended

Trang 2

In [1]: print("""

===========================

Simple Calculator

===========================

Please select an operation:

1) Add

2) Subtract

3) Multiply

4) Divide

""")

choice = input("Please enter a choice(1/2/3/4):")

num1 = int(input("Enter first number: "))

num2 = int(input("Enter second number: "))

if choice == '1':

print(num1,"+",num2,"=", num1+num2) elif choice == '2':

print(num1,"-",num2,"=", num1-num2) elif choice == '3':

print(num1,"*",num2,"=", num1*num2) elif choice == '4':

print(num1,"/",num2,"=", num1/num2) else:

print("Invalid input")

Python Program to Calculate Grade of Student

In [2]: mark = float(input("Please, enter your score: "))

if mark >= 90:

print('Your grade is : A+') elif mark >= 85:

print('Your grade is : A') elif mark >= 80:

print('Your grade is : B') elif mark >= 70:

print('Your grade is : C') elif mark >= 60:

print('Your grade is : D') else:

print('Failed! Your grade is : F')

===========================

Simple Calculator

===========================

Please select an operation:

1) Add

2) Subtract

3) Multiply

4) Divide

Please enter a choice(1/2/3/4):1

Enter first number: 4

Enter second number: 5

4 + 5 = 9

Please, enter your score: 80

Your grade is : B

Trang 3

If we use if instead of elif keywords; our program would not work properly.

In [4]: mark = float(input("Please, enter your score: "))

if mark >= 90:

print('Your grade is : A+')

if mark >= 85:

print('Your grade is : A')

if mark >= 80:

print('Your grade is : B')

if mark >= 70:

print('Your grade is : C')

if mark >= 60:

print('Your grade is : D') else:

print('Failed! Your grade is : F')

In [ ]:

Please, enter your score: 80

Your grade is : B

Your grade is : C

Your grade is : D

Ngày đăng: 17/11/2019, 07:34

TÀI LIỆU CÙNG NGƯỜI DÙNG

  • Đang cập nhật ...

TÀI LIỆU LIÊN QUAN