实验二 计算器设计

#20212115 2021-2022-2 《python程序设计》 实验报告二

课程:

课程:《Python程序设计》
班级: 2121
姓名: 朱时鸿
学号:20212115
实验教师:王志强
实验日期:2022年3月31日
必修/选修: 公选课

(一)实验内容

  • 设计并完成一个完整的应用程序,完成加减乘除模等运算,功能多多益善。

  • 考核基本语法、判定语句、循环语句、逻辑运算等知识点

(二)实验要求

创建工程项目,使用Python语言实现具体的操作运算,并完成程序调试和运行,代码托管到码云。

注:在华为ECS服务器(OpenOuler系统)和物理机(Windows/Linux系统)上使用VIM、IDLE、Pycharm等工具编程实现。

(三)实验报告

## 1.实验内容

1,学习到python语言中“+”作用为拼接,重复字符串为*

>>> s4="hello world"
>>> s4[:5:2]
'hlo'

2.学会使用split

>>> sl = "hello world 你好 世界"

>>> s1 = "hello world 你好 世界"
>>> s1.split()
['hello', 'world', '你好', '世界']

3.学会使用split(,)来消除字符

>>> s2="hello#world#你好#shijie"
>>> s2.split("#",1)
['hello', 'world#你好#shijie']

4.学会使用count find 函数来判断一个有几个字符

>>>s1.count("#")
>>>0

5.用upper 和 lower来时字母变大写或变小写

6.去掉字符串收尾的字符:strip(),lstrip(),rstrip();

默认去掉 空格 、\n  \t   \r回车等特殊字符

7.学会使用%操作符

-:使左对齐

+:右对齐

0:右对齐,复数前面加负号,用零填充

m:可选参数,表示占有宽度

8.学会了怎样表示复数

a=complex(input("第一个复数:\n"))
b=complex(input("第二个复数:\n"))

9.输出

print(complex1,op,complex2,"=",result)
print("a "+op+"b =",result,"\n")


10.循环

while(flag == True):
    choice = input("请选择你要选择的计算器类型:0为普通计算器,1为复数计算器:\n");
    op = input("请输入需要做的操作(+、-、*、/输入0代表退出):\n");

11.判断语句

if op == "0":
        flag = False;
        print("已退出,祝你生活愉快");
        break;
    if choice == "0":
        a = int(input("请输入操作数1:"));
        b = int(input("请输入操作数2:"));

## 2. 实验过程及结果
实验编程计算器的代码

import math;
flag = True;
print("besti专属计算器!");
while(flag == True):
    choice = input("请选择你要选择的计算器类型:0为普通计算器,1为复数计算器:\n");
    op = input("请输入需要做的操作(+、-、*、/输入0代表退出):\n");
    if op == "0":
        flag = False;
        print("已退出,祝你生活愉快");
        break;
    if choice == "0":
        a = int(input("请输入操作数1:"));
        b = int(input("请输入操作数2:"));
    elif choice == "1":
        a = complex(input("请输入操作数1:"));
        b = complex(input("请输入操作数2:"));
    result = 0;
    if op == "+":
        result = a + b;
    elif op == "-":
        result = a - b;
    elif op == "*":
        result = a * b;
    elif op == "/":
        result = a / b;
    else:
        print("输入错误,请重新输入\n");
        continue;
    print(str(a)+str(op)+str(b)+"="+str(result)+"\n");
    '''
    elif choice == "1":
        op = input("请输入需要做的操作(+、-、*、/输入0代表退出):\n");
        if op == "0":
            flag = False;
            print("已退出,祝你生活愉快");
            break;
        
        a1 = input("请输入第一个数字的实部:\n");
        a2 = input("请输入第一个数字的虚部:\n");
        b1 = input("请输入第二个数字的实部:\n");
        b2 = input("请输入第二个数字的虚部:\n");
        
        complex1 = complex(input("请输入第一个数:\n"));
        complex2 = complex(input("请输入第二个数:\n"));
        if op == "+":
            result = complex1 + complex2;
        if op == "-":
            result = complex1 - complex2;
        if op == "*":
            result = complex1 * complex2;
        if op == "/":
            result = complex1 / complex2;
        print(str(complex1)+op+str(complex2)+"="+str(result)+"\n");
        if op == "+":
            result1 = a1 + b1;
            result2 = b1 + b2;
        if op == "-":
            result1 = a1 - b1;
            result2 = b1 - b2;
        if op == "*":
            result1 = a1*b1 - a2*b2;
            result2 = a1*b2 + b1*a2;
        print(str(a1)+"+"+str(a2)+"i"+str(op)+str(b1)+"+"+str(b2)+"i="+str(result1)+"+"+str(result2));
    '''

再本次实验中的收获与重要的知识点在“内容板块”,最后的结果:成功编出计算器的代码并成功运行

## 3. 实验过程中遇到的问题和解决过程
- 问题1:出现缩进的问题
- 问题1解决方案:使用tap键
- 问题2:编程思路出现问题

if choice=="0":
a=int(input("输入a\n"))
b=int(input("输入b\n"))
elif choice=="1":
a=complex(input("第一个复数:\n"))
b=complex(input("第二个复数:\n"))

- 问题2解决方案:请教同学
- ...

## 其他(本次课上所写的计算器代码)

import math;
flag = True;
print("besti专属计算器!");
while(flag == True):
    choice = input("请选择你要选择的计算器类型:0为普通计算器,1为复数计算器:\n");
    op = input("请输入需要做的操作(+、-、*、/输入0代表退出):\n");
    if op == "0":
        flag = False;
        print("已退出,祝你生活愉快");
        break;
    if choice == "0":
        a = int(input("请输入操作数1:"));
        b = int(input("请输入操作数2:"));
    elif choice == "1":
        a = complex(input("请输入操作数1:"));
        b = complex(input("请输入操作数2:"));
    result = 0;
    if op == "+":
        result = a + b;
    elif op == "-":
        result = a - b;
    elif op == "*":
        result = a * b;
    elif op == "/":
        result = a / b;
    else:
        print("输入错误,请重新输入\n");
        continue;
    print(str(a)+str(op)+str(b)+"="+str(result)+"\n");
    '''
    elif choice == "1":
        op = input("请输入需要做的操作(+、-、*、/输入0代表退出):\n");
        if op == "0":
            flag = False;
            print("已退出,祝你生活愉快");
            break;
       
        a1 = input("请输入第一个数字的实部:\n");
        a2 = input("请输入第一个数字的虚部:\n");
        b1 = input("请输入第二个数字的实部:\n");
        b2 = input("请输入第二个数字的虚部:\n");
       
        complex1 = complex(input("请输入第一个数:\n"));
        complex2 = complex(input("请输入第二个数:\n"));
        if op == "+":
            result = complex1 + complex2;
        if op == "-":
            result = complex1 - complex2;
        if op == "*":
            result = complex1 * complex2;
        if op == "/":
            result = complex1 / complex2;
        print(str(complex1)+op+str(complex2)+"="+str(result)+"\n");
        if op == "+":
            result1 = a1 + b1;
            result2 = b1 + b2;
        if op == "-":
            result1 = a1 - b1;
            result2 = b1 - b2;
        if op == "*":
            result1 = a1*b1 - a2*b2;
            result2 = a1*b2 + b1*a2;
        print(str(a1)+"+"+str(a2)+"i"+str(op)+str(b1)+"+"+str(b2)+"i="+str(result1)+"+"+str(result2));

'''

感想:想学好python不易,需要自己付出努力以及遇见一位好老师,很幸运由王老师教课

下为码云的地址

https://gitee.com/zhu-shihong/zhu-shihongs-warehouse/commit/b84aafa140baa5847c177ea314ec0fc23ef7519e
## 参考资料

-  [《Java程序设计与数据结构教程(第二版)》](https://book.douban.com/subject/26851579/)

-  [《Java程序设计与数据结构教程(第二版)》学习指导](http://www.cnblogs.com/rocedu/p/5182332.html)
-  ...

20212115 实验二 《python程序设计》实验报告的更多相关文章

  1. 20175212童皓桢 Java实验二-面向对象程序设计实验报告

    20175212童皓桢 Java实验二-面向对象程序设计实验报告 实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉S.O.L.I.D原则 了解设 ...

  2. 20175316 盛茂淞 2018-2019-2 《Java程序设计》实验二 面向对象程序设计 实验报告

    20175316 盛茂淞 2018-2019-2 <Java程序设计>实验二 面向对象程序设计 实验报告 (一)单元测试 在 IDEA中我们把产品代码放在src目录中,把测试代码放在tes ...

  3. 20155220java实验二 面向对象程序设计 实验报告

    一.实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉S.O.L.I.D原则 了解设计模式 实验步骤 (一)单元测试 (1) 三种代码 伪代码 产 ...

  4. Python程序设计实验报告二:顺序结构程序设计(验证性实验)

      安徽工程大学 Python程序设计 实验报告 班级   物流191   姓名  崔攀  学号3190505136 成绩 日期     2020.3.22     指导老师       修宇 [实验 ...

  5. Python程序设计实验报告一:熟悉IDLE和在线编程平台

    安徽工程大学 Python程序设计 实验报告 班级   物流191   姓名  崔攀  学号3190505136 成绩_____           日期     2020.3.8     指导老师  ...

  6. Python程序设计实验报告四:循环结构程序设计(设计型实验)

    安徽工程大学 Python程序设计 实验报告 班级   物流191   姓名  姚彩琴  学号3190505129 成绩 日期     2020.4.8     指导老师       修宇 [实验名称 ...

  7. Python程序设计实验报告三:分支结构程序设计

    安徽工程大学 Python程序设计 实验报告 班级   物流191   姓名  姚彩琴  学号3190505129 成绩 日期     2020.4.5     指导老师       修宇 [实验目的 ...

  8. 2018-2019-2 20175306实验二面向对象程序设计《Java开发环境的熟悉》实验报告

    2018-2019-2 20175306实验二面向对象程序设计<Java开发环境的熟悉>实验报告 面向对象程序设计-1 实验要求: 参考:> http://www.cnblogs.c ...

  9. 2018-2019-20175205实验二面向对象程序设计《Java开发环境的熟悉》实验报告

    2018-2019-20175205实验二面向对象程序设计<Java开发环境的熟悉>实验报告 实验要求 没有Linux基础的同学建议先学习<Linux基础入门(新版)>< ...

  10. #2019-2020-4 实验二面向对象程序设计《Java开发环境的熟悉》实验报告

    2019-2020-4 实验二面向对象程序设计<Java开发环境的熟悉>实验报告 一.面向对象程序设计-1 ①实验要求: 1.参考 http://www.cnblogs.com/roced ...

随机推荐

  1. SVG vs Image, SVG vs Iconfont

    这可能是个别人写过很多次的话题,但貌似由于兼容性的原因?图标的显示还是用着 Iconfont 或者 CSS Sprite 的形式?希望通过自己新瓶装旧酒的方式能重新引导一下问题. SVG vs Ima ...

  2. vue2实现搜索结果中的搜索关键字高亮

    // 筛选变色 brightenKeyword(val, keyword) { val = val + ''; if (val.indexOf(keyword) !== -1 && k ...

  3. 你可以说出export export default || model.exports exports 的区别吗(一)

    一.前言: 用模块写代码,为什么要用模块来写代码:ES6之前,在js中定义的一切,都是共享一个全局作用域的,随着web应用变得复杂,这样做会引起如:命名冲突和安全问题.于是引入了模块. 二.清楚一个概 ...

  4. 【Android开发】Coding + git命令行基本使用

    上传代码 进入本地仓库的目录. cd ... 查看仓库链接 : git remote -v 如果没有,则添加url链接 : git remote add testName https://git.co ...

  5. C2678 二进制“<”: 没有找到接受“const ***”类型的左操作数的运算符解决办法

    正确代码如下:#include<iostream> #include<string> #include<map> using namespace std; /*仿函 ...

  6. Git使用方法以及出现的bug解决方案

    git常用命令 1.本地库初始化: git init 2.设置签名 (1)项目级别(项目里面) git config user.name xxx git config user.email xxx ( ...

  7. FastAPI(六十六)实战开发《在线课程学习系统》接口开发--用户注册接口开发

    在前面我们分析了接口的设计,那么我们现在做接口的开发. 我们先去设计下pydantic用户参数的校验 from pydantic import BaseModel from typing import ...

  8. Array实现

    (一)基本类型数组实现 public class Array { private int[] data; private int size; // 构造函数,传入数组的容量capacity构造Arra ...

  9. 缓存中间件-Redis(一)

    1.Redis介绍 REmote DIctionary Server(Redis) 是一个由Salvatore Sanfilippo写的 key-value 存储系统,是跨平台的非关系型数据库,Red ...

  10. for .. range中的坑

    最近在开发中使用了for range来遍历一个slice,结果在测试的时候遇到了bug,最后定位是错误使用for range造成的,这里记录一下: func redisSlaveScanBigKeys ...