Python 实验2 选择结构程序设计
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
实验2 选择结构程序设计
1、预测你的小孩的身高男性身高=(父亲身高+母亲身高)×1.08÷2(厘米)女性身高=(父亲身高×0.923+母亲身高)÷
2(厘米)
#predicting your child's height
print "The following calculating unit is cm."
x=input("Please enter dad's height:",)
y=input("Please enter mom's height:",)
g=raw_input("Please enter your child's gender:")
if g=="male":
h=(x+y)*1.08/2
print h,"cm"
if g=="female":
h=(x*0.923+y)/2
print h,"cm"
2、输入一个年份,判断它是否为闰年,并输出是否为闰年的相关信息。
【提示】判断闰年的条件是:年份能被4整除但不能被100整除;或者是能被400整除。如:1900、2100、2010年不是闰年;2008、2000年是闰年。
y=input("Please enter the year:")
if y%4==0 and y%100!=0 or y%400==0:
print y,"是闰年"
else:
print y,"不是闰年"
3、输入一个学生的成绩,如果是90分以上,打印出“A”的评语;80分以上的,打印出“B”;70分以上的,打印出“C”;
60分以上的,打印出“D”;不及格的打印出”E”。
print "This is a procedure which can print the mark related to your score. The range of your score is 0~100."
x=input("Please enter your score:")
while True:
if x<0 or x>100:
x=input("Wrong input, the range is 0~100. Please enter again:")
if 90 print "A" break if 80 print "B" break if 70 print "C" break if 60 print "D" break if 0<=x<60: print "E" break 4、通过InputBox函数任意输入三条边长,经过简单的计算后,判断三条边长能否构成三角形,并在文本框中显示结果。 【提示】构成三角形,必须保证任意两边的和大于第三边 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 b+c>a and a+c>a: print "三条边可以构成三角形" else: print "三条边不可构成三角形" 5、输入一个字符,判断该字符是大写字母、小写字母,数字还是其他字符,并作相应的显示。 提示:利用ord()函数来获得字符的ASCII。 x=raw_input("Please input a character:") if 48<=ord(x)<=57: print "The character is a number." elif 65<=ord(x)<=90: print "The character is a capital letter." elif 97<=ord(x)<=122: print "The character is a lowercase letter." else: print "This belongs to other characters." 6、企业发放的奖金根据利润提成。利润(I)低于或等于10万元时,奖金可提10%;利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可提成7.5%;20万到40万之间时,高于20万元的部分,可提成5%;40万到60万之间时高于40万元的部分,可提成3%;60万到100万之间时,高于60万元的部分,可提成1.5%,高于100万元时,超过100万元的部分按1%提成,从键盘输入当月利润I,求应发放奖金总数? I=input("Please input the interest(Unit:ten thousand):") if I<=10: print "The prize should be:",0.1*I,"(ten thousand)" if 10 print "The prize should be:",(I-10)*0.075+10*0.1,"(ten thousand)" if 20 print "The prize should be:",(I-20)*0.05+10*0.1+10*0.075,"(ten thousand)" if 40 print "The prize should be:",(I-40)*0.03+10*0.1+10*0.075+20*0.05,"(ten thousand)" if 60 print "The prize should be:",(I-60)*0.015+10*0.1+10*0.075+20*0.05+20*0.03,"(ten thousand)" if I>100: print "The price should be:",(I-100)*0.01+10*0.1+10*0.075+20*0.05+20*0.03+40*0.015,"(ten thousand)" 7、密码登录程序。要求:建立一个登录窗口,要求输入帐号和密码。设定密码为“Python123”;若密码正确,如果是男生,则显示“祝贺你,某某先生,成功登录!”; 如果是女生,则显示“祝贺你,某某女士,成功登录!”;若密码不正确,显示“对不起,密码错误,无法登录!”。 x=raw_input("User:") y=raw_input("Password:") g=raw_input("Gender(male or female):") if y=="Python123": if g=="male": print "Congratulations,Mr",x,".You have logged-in successfully!" if g=="female": print "Congratulations,Ms",x,".You have logged-in successfully!" else: print "Sorry,wrong password.Unable to log-in."