93
社区成员
发帖
与我相关
我的任务
分享课程:《Python程序设计》
班级: 2241
姓名: 童昊
学号:20224104
实验教师:王志强
实验日期:2024年3月26日
必修/选修: 专选课
设计并完成一个完整的应用程序,完成加减乘除模等运算,功能多多益善。
考核基本语法、判定语句、循环语句、逻辑运算等知识点。
用def创建函数,完成加减乘除以及复数运算的编写;之后使用“if……elif……”语句将多种运算整合到一个程序中,用“while”语句,实现计算器的多次使用。最后,点击“run”选项运行程序,计算器运行无误,完成程序的编写。
首先,在pycharm中新建一个程序

接着编写代码
import math
def sum (a,b):
return(a+b)
def sub (a,b):
return(a-b)
def mul (a,b):
return(a*b)
def div (a,b):
if b == 0:
print("o不能做除数")
return 0
return(a/b)
def csum (c1,c2):
return c1+c2
def csub (c1,c2):
return c1-c2
def cmul (c1,c2):
return c1*c2
def cdiv (c1,c2):
return c1/c2
def log(a,b):
return math.log(b,a)
flag = True
while flag:
print("""
Welcome to this calculator that can do
addition, subtraction, multiplication, division, complex and logarithmic operations
producer:20224104 童昊
time:2024/3/26
let's start now
""")
operator=input("请输入运算符号(+、-、*、/、c、log)")
if operator == "+":
a = int(input("请输入a="))
b = int(input("请输入b="))
print("a+b=",sum(a,b))
elif operator == "-":
a = int(input("请输入a="))
b = int(input("请输入b="))
print("a-b=",sub(a,b))
elif operator == "*":
a = int(input("请输入a="))
b = int(input("请输入b="))
print("a*b=", mul(a, b))
elif operator == "/":
a = int(input("请输入a="))
b = int(input("请输入b="))
print("a/b=", div(a, b))
elif operator == "c":
coperator = input("请输入复数的运算+-*、:")
c1 = eval(input("请输入复数1(eg:1+2j):"))
c2 = eval(input("请输入复数2(eg:1+2j):"))
if coperator == "+":
print("(",c1,")+(",c2,")=", csum(c1,c2))
elif coperator == "-":
print("(",c1,")-(",c2,")=", csub(c1,c2))
elif coperator == "*":
print("(", c1, ")*(", c2, ")=", cmul(c1, c2))
elif coperator == "/":
print("(", c1, ")/(", c2, ")=", cdiv(c1, c2))
elif operator == "log":
a = eval(input("请输入a:"))
b = int(input("请输入b:"))
print("logab=", log(a, b))
flag= True if eval("是否继续游戏?Y or N")== "Y" else False
运行程序,进行加减乘除等运算
加法

减法

乘法

除法

复数

对数

最后,同步到gitee

问题:没有很好掌握代码编写及具体语法、语句应用
解决方案:回顾老师上课的讲解以及编写的步骤,我又翻看了《零基础学python》这本书,更深刻的理解了语法、语句的使用,并且我积极寻求同学帮助,最终取得成功
在编写代码的过程中,我不断回顾和巩固了基本语法、判定语句、循环语句以及逻辑运算等知识点。每一个功能的实现,都是对这些知识点的综合运用和深化理解。我逐渐领悟到,编程不仅仅是编写代码,更是一种逻辑思维的锻炼和解决问题的艺术。加减乘除模等基本运算的实现,虽然看似简单,但在实际应用中却需要细心和耐心。我不断调试和优化代码,确保程序的稳定性和准确性。知行合一,对于老师上课的讲解以及我的实践,我深感道路曲折却又增加了克服困难的挑战性与趣味,感谢老师的付出
《零基础学python》