python实验三选择结构_实验1:选择结构

这篇博客介绍了Python选择结构的使用,包括If-Then、If-Then-Else、If-Then-Else-If以及Switch/Case语句。通过三个示例,展示了如何运用关系和逻辑运算符来构建条件控制。实验内容涉及防御性编程和模块化编程,以及闰年的判断。最后,博主分享了实验过程、结果分析以及对选择结构的理解和未来学习计划。
摘要由CSDN通过智能技术生成

实验目的

了解选择结构的含义,区分单一选项、双选项和多选项选择结构的不同和构造方法。

熟练使用关系运算符和逻辑运算符构造选择控制条件。

基于选择结构实践并掌握防御性编程和模块化编程的基本程序框架。

实验知识点:使用IDLE编写Python程序

If-Then, If-Then-Else,If-Then-Else-If以及Switch/Case语句的使用

关系运算符与逻辑运算符

Python相关语法

问题分析与程序设计

伪代码:

1.

Write “please enter a number”

Input number

If number>0

Write “positive”

Else if number<0

Write “negative”

Else

Write “zero”

2.

Declare earn As Float

Declare fax As Float

Input earn

If(earn>=0)AND(earn<50000)Then

Set fax=earn*0.05

Else If(earn>=50000)AND(earn<100000)Then

Set fax=2500+(earn-50000)*0.07

Else If earn>=100000 Then

Set fax=6000+(earn-100000)*0.09

Else

Write “fault”

End If

Write fax

3.

Declare year As Integer

Declare month As Integer

Declare sum As Integer

Input year,month,day

Select Case of month

Case 1

sum=0

Break

Case 2

sum=31

Break

Case 3

sum=59

Break

Case 4

sum=90

Break

Case 5

sum=120

Break

Case 6

sum=151

Break

Case 7

sum=181

Break

Case 8

sum=212

Break

Case 9

sum=243

Break

Case 10

sum=273

Case 11

sum=304

Case 12

sum=334

End Case

If(year%4==0)AND(month>2)Then

sum=sum+day+1

Else If year%4!=0

sum=sum+day

Else

sum=sum+day

End If

Write “year”+year+ “day”+sum

①打出十二个判断循环

②在每个循环中进行判断闰年

##code:

题目一、

number=eval(input("please enter a number"))

if number>0:

print("Positive")

elif number<0:

print("Negative")

else:

print("Zero")

题目二、

money=eval(input("please enter your money"))

if money>=0 and money<50000:

a=money*0.05

elif money>=50000 and money<100000:

a=2500+(money-50000)*0.07

elif money<0:

a=6000+(money-100000)*0.09

print("your tax",a)

题目三、

year=eval(input("please enter the year:"))

month=eval(input("please enter the month:"))

day=eval(input("please enter the day:"))

i=0

Days=0

if year%4==0 and year%100!=0:

day[2]=29

while i

Days=Days+Days[i]

i=i+1

print("day",Days+day,"year",year)

c26b5656f306303706b941e9df4434f0.png

93445ff6ecff19f8b1e074bfe8d912cf.png

266c3047983c5454234614df89c79b6a.png

974a87fe6de38b202b20d1d02e8112c2.png

234d40c179dcd7a733a2b2fa0ad51873.png

5f5b4534eb14e1fed12c7b3d09609cd8.png

#实现过程与测试结果分析

过程较为复杂但是运算结果正确

#实验结果总结

实验完成较好

#创新部分和对实验的意见与建议

明白了选择结构,继续努力。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值