用python代码举例讲解一下
时间: 2023-03-14 20:05:37 浏览: 81
python 代码案例
Python是一种面向对象的编程语言,可以用来创建很多不同类型的应用程序。例如,要使用Python编写一个计算器应用程序,可以使用以下代码:def add(x, y):
return x + ydef subtract(x, y):
return x - ydef multiply(x, y):
return x * ydef divide(x, y):
return x / ynum1 = int(input("请输入第一个数字:"))
num2 = int(input("请输入第二个数字:"))print("选择运算:")
print("1.加")
print("2.减")
print("3.乘")
print("4.除")choice = input("输入你的选择(1/2/3/4):")if choice == '1':
print(num1,"+",num2,"=", add(num1,num2))elif choice == '2':
print(num1,"-",num2,"=", subtract(num1,num2))elif choice == '3':
print(num1,"*",num2,"=", multiply(num1,num2))elif choice == '4':
print(num1,"/",num2,"=", divide(num1,num2))else:
print("无效的输入")
阅读全文