1. 程式人生 > 實用技巧 >python條件語句多條件

python條件語句多條件

使用Python進行測試的條件語句與我們編寫的其他程式語言相似,在採取任何決定之前,if和else語句將根據某些條件獲得所需的結果。

如何在Python示例中使用條件語句,我們使用的是Mint作業系統,該作業系統的Python版本為2.7.6,Python版本為3.4.0,但條件語句不依賴於作業系統,因此我們可以使用任何OS。此條件語句也適用於早期的Python版本。

Using username "sloba".
sloba@***.***.*.*'s password:
Welcome to Linux Mint 17.2 Rafaela (GNU/Linux 3.16.0-38-generic x86_64)
Welcome to Linux Mint
 * Documentation: http://www.linuxmint.com
Last login: Sun Oct 11 18:25:32 2015 from ***.***.*.*
sloba@sloba-VirtualBox ~ $ 
loba@sloba-VirtualBox ~ $ python2.7
Python 2.7.6 (default, Mar 22 2014, 22:59:56)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
sloba@sloba-VirtualBox ~ $ python3
Python 3.4.0 (default, Apr 11 2014, 13:05:11)
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.

>>>

Python 2.7.6 (default, Mar 22 2014, 22:59:56)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> a =2
>>> b =5
>>>

 

sloba@sloba-VirtualBox ~/Desktop/myscripts $ python
Python 2.7.6 (default, Mar 22 2014, 22:59:56)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> a =2
>>> b =5
>>>
>>> a,b = 2,5
>>>
>>> print a
2
>>> print b
5
>>>

>>>
>>> if a < b:
... print a, " is cannot be more than ",b
...
2 is cannot be more than 5
>>>

if語句以另一個if結束,就像我們在其他程式語言中所做的那樣,因此,只寫了以下幾行,並且在三個點後使用TAB表示我的右縮排。按Enter鍵檢查條件語句中的操作符少於  

>>> if a < b:
...     print a, " is cannot be more than ",b
...

>>> if a>b:
...     print a," is greater than ",b
...
>>>

>>> if a>b:
...     print a," is greater than ",b
... else:
...     print a," is not greater than ",b
...
2  is not greater than 5
>>>

if和else語句使用運算子。

為了建立條件語句,我將使用一個數學遊戲來僅使用條件語句。要計算單利,我們需要知道本金,這是我們將要使用的起始金額,該金額的利率和時間。要計算的公式是i = prt

我的意思是賺取利息

  • P表示本金
  • R是利率
  • T是時間


Sloba賺取的利息為1,600盧比,包括本金在內的總金額為41,600.00。讓我們在名為“ simple_interest.py”的指令碼中進行設定。  

#!/usr/bin/python
#Author: Swadhin
# Date: 11 - Oct - 2015
# Purpose: Simple interest game in Python
 
print "--------------Simple interest game--------------"
print "------------------------------------------------"
#Below are the variables used to calculate the formula
principle = 40000
# rate 4 % taken as per the question
rate = 4
# time in year
time = 1
name = raw_input("What is your name:")
print "Welcome ", name
print "Here is the question: "
print "Say Sloba has 40,000 in rupees in his savings account where he has earned interest of 4% annually which is not compounded."
answer = input("So the question would be how much he will save in 1 years. ")
si = (principle * rate * time) / 100
if answer == si:
    print name, "you have given the correct answer, the total saving Sloba would save in rupees is 40000 plus 1600 i.e.41,600 in one year "
else :
    print name, "your answer is not correct"

sloba@sloba-VirtualBox ~/Desktop/myscripts $ ls
simple_interest.py
sloba@sloba-VirtualBox ~/Desktop/myscripts $ python simple_interest.py
--------------Simple interest game--------------

------------------------------------------------
What is your name: Ray
Welcome Ray
Here is the question:
Say Sloba has 40,000 in rupees in his savings account where he has earned interest of 4% annually which is not compounded.
So the question would be how much he will save in 1 years. 1400
 Ray your answer is not correct
sloba@sloba-VirtualBox ~/Desktop/myscripts $

sloba@sloba-VirtualBox ~/Desktop/myscripts $ python simple_interest.py
--------------Simple interest game--------------
------------------------------------------------
What is your name: Ray
Welcome Ray
Here is the question:
Say Sloba has 40,000 in rupees in his savings account where he has earned interest of 4% annually which is not compounded.
So the question would be how much he will save in 1 years. 1600
 Ray you have given the correct answer, the total saving Sloba would save in rupees is 40000 plus 1600 i.e.41,600 in one year
sloba@sloba-VirtualBox ~/Desktop/myscripts $

最後的指令碼:  

#!/usr/bin/python
#Author: Swadhin
# Date: 11 - Oct - 2015
# Purpose: Simple interest game in Python
print "--------------Simple interest game--------------"
print "------------------------------------------------"
#Below are the variables used to calculate the formula
principle = 40000
#rate 4 % taken as per the question
rate = 4
# time in year
time = 1
name = raw_input("What is your name:")
print "Welcome ", name
print "Here is the question: "
print "Say Sloba has 40,000 in rupees in his savings account where he has earned interest of 4% annually which is not compounded."
answer = input("So the question would be how much he will save in 1 years. ")
si = (principle * rate * time) / 100
if answer == si:
    print name, "you have given the correct answer, the total saving Sloba would save in rupees is 40000 plus 1600 i.e.41,600 in one year "
elif answer >=1500 and answer <=1599: 
  print name, "your answer is very close, so try again"
elif answer >1600:
  print name, "you have exceeded the actual value of the correct answer, so try again"
else :
    print name, "your answer is not correct"

現在,讓我們繼續測試Python中的條件語句。下面的測試顯示了我們如何使用python中的if和else語句來實現其他邏輯。

sloba@sloba-VirtualBox ~/Desktop/myscripts $ clear
sloba@sloba-VirtualBox ~/Desktop/myscripts $ python simple_interest.py

--------------Simple interest game--------------
------------------------------------------------
What is your name: Ray
Welcome Ray
Here is the question:
Say Sloba has 40,000 in rupees in his savings account where he has earned intere st of 4% annually which is not compounded.
So the question would be how much he will save in 1 years. 500
 Ray your answer is not correct
sloba@sloba-VirtualBox ~/Desktop/myscripts $ python simple_interest.py
--------------Simple interest game--------------
------------------------------------------------
What is your name: Ray
Welcome Ray
Here is the question:
Say Sloba has 40,000 in rupees in his savings account where he has earned intere st of 4% annually which is not compounded.
So the question would be how much he will save in 1 years. 1599
 Ray your answer is very close, so try again
sloba@sloba-VirtualBox ~/Desktop/myscripts $ python simple_interest.py
--------------Simple interest game--------------
------------------------------------------------
What is your name: Ray
Welcome Ray
Here is the question:
Say Sloba has 40,000 in rupees in his savings account where he has earned intere st of 4% annually which is not compounded.
So the question would be how much he will save in 1 years. 1650
 Ray you have exceeded the actual value of the correct answer, so try again
sloba@sloba-VirtualBox ~/Desktop/myscripts $ python simple_interest.py
--------------Simple interest game--------------
------------------------------------------------
What is your name: Ray
Welcome Ray
Here is the question:
Say Sloba has 40,000 in rupees in his savings account where he has earned interest of 4% annually which is not compounded.
So the question would be how much he will save in 1 years. 1600
 Ray you have given the correct answer, the total saving Sloba would save in rupees is 40000 plus 1600 i.e.41,600 in one year
sloba@sloba-VirtualBox ~/Desktop/myscripts $