How to Use If-elif-else in Python


In this example, we will learn how to display price judgment based on the sum of item prices in Python.

Source Code

#! /usr/bin/env python3
# -*- coding: utf-8 -*-

price1 = float(input("Please input the price of the 1st item: "))
price2 = float(input("Please input the price of the 2nd item: "))
price3 = float(input("Please input the price of the 3rd item: "))
price4 = float(input("Please input the price of the 4th item: "))
price5 = float(input("Please input the price of the 5th item: "))
sum1 = price1 + price2 + price3 + price4 + price5
if sum1 >= 1000:
    print("Over the budget!")
elif sum1 >= 800 and sum1 < 1000: 
    print("Need negotiate") 
elif sum1 >= 700 and sum1 < 800: 
    print("Not bad, maybe you can buy them") 
elif sum1 >= 600 and sum1 < 700: 
    print("Buy!") 
elif sum1 >= 500 and sum1 < 600:
    print("Any problem?")
else:
    print("Too cheap, should be careful")

Output:

Please input the price of the 1st item: 200
Please input the price of the 2nd item: 180
Please input the price of the 3rd item: 170
Please input the price of the 4th item: 185
Please input the price of the 5th item: 190
Need negotiate
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
Advertisement