当前位置:文档之家› 最新Python 实验2 选择结构程序设计

最新Python 实验2 选择结构程序设计

1
实验2 选择结构程序设计
2
1、预测你的小孩的身高男性身高=(父亲身高+母亲身高)×1.08÷2(厘米)女性身高=(父亲身高3
×0.923+母亲身高)÷2(厘米)
4
#predicting your child's height
5
print "The following calculating unit is cm."
6
x=input("Please enter dad's height:",)
7
y=input("Please enter mom's height:",)
8
g=raw_input("Please enter your child's gender:")
9
if g=="male":
h=(x+y)*1.08/2
1
print h,"cm"
2
if g=="female":
3
h=(x*0.923+y)/2
4
print h,"cm"
5
2、输入一个年份,判断它是否为闰年,并输出是否为闰年的相关信息。

6
【提示】判断闰年的条件是:年份能被4整除但不能被100整除;或者是能被400整除。

如:1900、2100、7
2010年不是闰年;2008、2000年是闰年。

8
y=input("Please enter the year:")
9
if y%4==0 and y%100!=0 or y%400==0:
print y,"是闰年"
1
else:
2
print y,"不是闰年"
3
3、输入一个学生的成绩,如果是90分以上,打印出“A”的评语;80分以上的,打印出“B”;4
70分以上的,打印出“C”;60分以上的,打印出“D”;不及格的打印出”E”。

5
print "This is a procedure which can print the mark related to your score. The range 6
of your score is 0~100."
7
x=input("Please enter your score:")
8
while True:
9
if x<0 or x>100:
x=input("Wrong input, the range is 0~100. Please enter again:")
1
if 90<x<=100:
2
print "A"
3
break
4
if 80<x<=90:
5
print "B"
6
break
7
if 70<x<=80:
8
print "C"
9
break
if 60<x<=70:
1
print "D"
2
break
3
if 0<=x<60:
4
print "E"
5
break
6
4、通过InputBox函数任意输入三条边长,经过简单的计算后,判断三条边长能否构成三角形,并在文7
本框中显示结果。

8
【提示】构成三角形,必须保证任意两边的和大于第三边
9
a=input("Please input the length of a:")
b=input("Please input the length of b:")
1
c=input("Please input the length of c:")
2
if a+b>c and b+c>a and a+c>a:
3
print "三条边可以构成三角形"
4
else:
5
print "三条边不可构成三角形"
6
5、输入一个字符,判断该字符是大写字母、小写字母,数字还是其他字符,并作相应的显示。

7
提示:利用ord()函数来获得字符的ASCII。

8
x=raw_input("Please input a character:")
9
if 48<=ord(x)<=57:
print "The character is a number."
1
elif 65<=ord(x)<=90:
2
print "The character is a capital letter."
3
elif 97<=ord(x)<=122:
4
print "The character is a lowercase letter."
5
else:
6
print "This belongs to other characters."
7
6、企业发放的奖金根据利润提成。

利润(I)低于或等于10万元时,奖金可提10%;利润高于10万元,8
低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可提成7.5%;20万到40万之间9
时,高于20万元的部分,可提成5%;40万到60万之间时高于40万元的部分,可提成3%;60万到100 0
万之间时,高于60万元的部分,可提成1.5%,高于100万元时,超过100万元的部分按1%提成,从键盘1
输入当月利润I,求应发放奖金总数?
2
I=input("Please input the interest(Unit:ten thousand):")
3
if I<=10:
4
print "The prize should be:",0.1*I,"(ten thousand)"
5
if 10<I<=20:
6
print "The prize should be:",(I-10)*0.075+10*0.1,"(ten thousand)"
7
if 20<I<=40:
8
print "The prize should be:",(I-20)*0.05+10*0.1+10*0.075,"(ten thousand)"
9
if 40<I<=60:
print "The prize should be:",(I-40)*0.03+10*0.1+10*0.075+20*0.05,"(ten thousand)"
1
if 60<I<=100:
2
print "The prize should be:",(I-60)*0.015+10*0.1+10*0.075+20*0.05+20*0.03,"(ten
3
thousand)"
4
if I>100:
5
print "The price should
6
be:",(I-100)*0.01+10*0.1+10*0.075+20*0.05+20*0.03+40*0.015,"(ten thousand)"
7
7、密码登录程序。

要求:建立一个登录窗口,要求输入帐号和密码。

设定密码为“Python123”;若密码8
正确,如果是男生,则显示“祝贺你,某某先生,成功登录!”; 如果是女生,则显示“祝贺你,某某女士,9
成功登录!”;若密码不正确,显示“对不起,密码错误,无法登录!”。

x=raw_input("User:")
1
y=raw_input("Password:")
2
g=raw_input("Gender(male or female):")
3
if y=="Python123":
4
if g=="male":
5
print "Congratulations,Mr",x,".You have logged-in successfully!"
6
if g=="female":
7
print "Congratulations,Ms",x,".You have logged-in successfully!"
8
else:
9
print "Sorry,wrong password.Unable to log-in." 0。

相关主题