我是靠谱客的博主 淡然唇膏,最近开发中收集的这篇文章主要介绍python条件语句多条件_Python中的条件语句,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

python条件语句多条件

Conditional statements for testing using Python is similar to any other programming language in that we write

使用Python进行测试的条件语句与我们编写的任何其他编程语言相似

if and else statements to get the desired results based on certain conditions before acting on any decision.

在采取任何决定之前, ifelse语句将根据某些条件获得所需的结果。

To show how to use conditional statements with example in Python I am using Mint operating system which has Python version 2.7.6 and also Python version 3.4.0 but the conditional statements are not operating system dependent , so we can use any OS. This conditional statements works on earlier Python versions too. 

为了展示如何在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 statements by ending with another if like we do in other programming languages, so I have only written the below lines and after three dots used TAB for my right indentation. Hit enter to check conditional statements with less than operators:

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 and else statements using operators.

ifelse语句使用运算符。

To make the conditional statements I am going to use one mathematical game just to make use of conditional statements. To calculate simple interest we need to know the principal which is the starting amount that we are going to use, the interest rate for the amount, and the time. The formula to calculate is i = prt .

为了创建条件语句,我将使用一个数学游戏来仅使用条件语句。 要计算单利,我们需要知道本金,这是我们将要使用的起始金额,该金额的利率和时间。 要计算的公式是i = prt

  • I means interest earned

    我的意思是赚取利息
  • P means principal amount

    P表示本金
  • R is the rate of interest

    R是利率
  • T is time

    T是时间
The interest earned by Sloba is 1,600 in rupees and the total amount including the principal is 41,600.00.
Let’s make this in a script named "simple_interest.py".
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 $

The final script:

最后的脚本:

#!/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"

Now let’s go ahead and test the conditional statements in Python. And the below test shows how we can use if and else statements in python to implement additional logics. 

现在,让我们继续测试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 $

翻译自: https://www.experts-exchange.com/articles/21639/Conditional-statements-in-Python.html

python条件语句多条件

最后

以上就是淡然唇膏为你收集整理的python条件语句多条件_Python中的条件语句的全部内容,希望文章能够帮你解决python条件语句多条件_Python中的条件语句所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(39)

评论列表共有 0 条评论

立即
投稿
返回
顶部