初学python-练习_3使用python编写简单四则运算计算器

本文介绍了一个使用Python实现的简易计算器程序,该程序支持基本的加、减、乘、除运算,并通过用户输入进行操作数和运算符的选择。用户可以选择是否继续进行新的计算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#!/usr/bin/python
#-*- coding:UTF-8 -*-   #set chinese code

#definition four function
def COUNT(count_one,count_tuo,operation):
    if operation == '+':
        print count_one,'+',count_tuo,'=',(count_one + count_tuo);
    elif operation == '-':
        print count_one,'-',count_tuo,'=',(count_one - count_tuo);
    elif operation == '*' or operation == 'x'\
            or operation == 'X' or operation == '×':
        print count_one,'*',count_tuo,'=',(count_one * count_tuo);
    elif operation == '/' or operation == '÷':
        print count_one,'/',count_tuo,'=',(count_one / count_tuo);
    elif operation == '%':
        print count_one,'%',count_tuo,'=',(count_one % count_tuo);
    else:
        return 1;   "error"   

#give way user input operand
while_condition = True;
while while_condition:
    count_one = int(input("请输入第一个数:"));
    count_tuo = int(input("请输入第二个数:"));
    operation = raw_input("请输入要执行的四则运算(+-*/):");
    if COUNT(count_one,count_tuo,operation) == 1:
      print "error operation";
    else:
        while True:
            Continue = raw_input("是否继续?y/n:");
            if Continue == 'y':
                 print "user continue";
                 break;
            elif Continue == 'n':
                 print "user termination";
                 while_condition = False;
                 break;
            else:
                print "user input error the option"

运行输出:

 

请输入第一个数:567
请输入第二个数:123
请输入要执行的四则运算(+-*/):+
567 + 123 = 690
是否继续?y/n:y
user continue
请输入第一个数:1376
请输入第二个数:98
请输入要执行的四则运算(+-*/):-
1376 - 98 = 1278
是否继续?y/n:s
user input error the option
是否继续?y/n:n
user termination

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

17岁boy想当攻城狮

感谢打赏

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值