python实验3选择结构程序设计

  • 格式:doc
  • 大小:19.50 KB
  • 文档页数:2

下载文档原格式

  / 5
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

实验3 选择结构程序设计(续)

【实验目的】

1.掌握分支条件语句的使用。

2.掌握分支嵌套语句的使用。

【实验内容】

1.通过Input()函数任意输入三条边长,经过简单的计算后,判断三条边长能否构成三角

形,并确定是类型的三角形,如(等边,等腰,一般三角形)。

a=input("Please input the length of a:")

b=input("Please input the length of b:")

c=input("Please input the length of c:")

if a+b>c and a+c>b and b+c>a:

if a==b==c:

print "This is an equilateral triangle."

if a==b or a==c or b==c:

print "This is an isosceles triangle."

if a!=b!=c:

print "This is a scalene triangle."

else:

print "These lengths can not form a triangle."

2.密码登录程序。要求:建立一个登录窗口,要求输入帐号和密码。设定用户名

为”zhangshan”,密码为“Python123”;若用户名正确,密码正确,则显示“Zhangshan 先生,欢迎你!”;如果用户名错误,则显示“用户名错误,请重新输入!”;若密码不正确,显示“对不起,密码错误,无法登录!”。

x=raw_input("User:")

y=raw_input("Password:")

if x=="zhangshan" and y=="Python123":

print "Welcome,!"

if x=="zhangshan" and y!="Python123":

print "Wrong right to log-in."

while x!="zhangshan" and y=="Python123":

x=raw_input("Wrong user's enter again:")

if x=="zhangshan":

print "Zhangshan先生,欢迎你!"

3.设有三个变量a,b,c,分别对三个变量赋值,并对三个变量进行排序。如a=5,b=7,c=6,则

排序结果为b>c>a。

a=input("Assign 'a' a value:")

b=input("Assign 'b' a value:")

c=input("Assign 'c' a value:")

if a>b>c:

print "a>b>c"

if a>c>b:

print "a>c>b"

if b>a>c:

print "b>a>c"

if b>c>a:

print "b>c>a"

if c>a>b:

print "c>a>b"

if c>b>a:

print "c>b>a"

4.计算一元二次方程 ax2+bx+c 的根是公式。因为负数的平方根是虚的,所以可以使用平方

根里面的表达式(称为差别式)先进地判别,检查根型。如果判别式是负数,根是虚的。

如果判别式是零,只有一个根;如果判别式是正的,有两个根。写一个程序,使用二次方根式得到实根,即忽略虚根。使用判别式确定有一个根或两个根,然后显示出答案。print "a*x**2+b*x+c=0"

a=input("Input 'a':")

b=input("Input 'b':")

c=input("Input 'c':")

if b**2-4*a*c>0:

x1=-b+(b**2-4*a*c)**(2*a)

x2=-b-(b**2-4*a*c)**(2*a)

print "The number of the root of equation:2",x1,x2

if b**2-4*a*c==0:

x0=-b/(2*a)

print "The number of the root of equation:1",x0

if b**2-4*a*c<0:

print "Non-real complex roots."