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

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

原文 http://blog.csdn.net/bjbz_cxy/article/details/79231185

2018-02-02 02:01:06阅读(237)

#!/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


最新发布

CentOS专题

关于本站

5ibc.net旗下博客站精品博文小部分原创、大部分从互联网收集整理。尊重作者版权、传播精品博文,让更多编程爱好者知晓!

小提示

按 Ctrl+D 键,
把本文加入收藏夹