Python创建计算器 [英] Python creating a calculator

查看:163
本文介绍了Python创建计算器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对python还是很陌生。

I am fairly new to python.

我被要求仅使用字符串命令,int / string / float等之间的转换来创建计算器(如果需要) ),并且必须使用功能。

I have been asked to create a calculator using only string commands, conversions between int/string/float etc.(if needed), and using functions is a requirement. while and for loops can also be used.

程序需要采用x / y或x / y / z形式的输入,其中xyz为正数或负数。其中 /也可以用加法乘除法代替。并且在操作数和运算符之间可以存在任意数量的空格。

The program needs to take an input of the form x/y or x/y/z, where x y z are any positive or negative number. Where "/" can be replaced by addition multiplication and subtraction as well. And where any number of white spaces can exist between operands and operators. This is an idea of what I have so far.

我将对+,-,/和*有一个唯一的定义。我将为用户输入的内容创建一个函数。我将使用 .lstrip和 .rstrip来消除空格。

I would have a unique definition for +,-,/, and *. I would create a function for what the user inputs. I would use ".lstrip" and ".rstrip" to get rid of white spaces.

现在,我遇到的麻烦是创建输入函数。我对函数非常陌生,基本上这就是我所拥有的。我知道可以使用的功能并不多,但是我真的对如何正确输入该函数感到困惑。

Now what I am having trouble with is creating the input function. I am very new to functions and this is basically what I have. I know it isn't much to work with but I am really stuck on how to properly enter the function.

def multiplication(x,a,y,b,z):
    if (a== "*"):
        return x*y
    if (b== "*"):
        return y*z

def division(x,a,y,b,z):
    if (a== "/"):
        return x/y
    if (b== "/"):
        return y/z

def addition(x,a,y,b,z):
    if (a== "+"):
        return x+y
    if (b== "+"):
        return y+z

def subtraction(x,a,y,b,z):
    if (a== "-"):
        return x-y
    if (b== "-"):
        return y-z

def (x,y,z):
    x=0
    y=0
    z=0

    zxc=int(input()):# this is where I get stuck and I don't know how to implement x,y,z into the input.

感谢所有帮助。如果不确定所提供的代码是否满足我的需求,请在浪费我的时间之前进行询问,以使我无法使用这些代码。我保证会尽快答复。

All help is appreciated. If you are unsure of whether the code you provide is too intense for my needs, please ask before wasting your time for me, making code that I can't possibly use. I promise to reply ASAP.

基本上,我试图找到一种方法来分割输入的字符串,然后用它开始计算。

Basically I am trying to find a way to split the inputted string AND THEN start calculations with it.

推荐答案

下面是使用正则表达式的可能解决方案概述。错误检查留作练习。如果这不是家庭作业,并且您希望看到充实的解决方案,请在此处查看

Here is a possible solution outline using regular expressions. Error checking left as exercise. If this isn't homework and you'd like to see the fleshed-out solution, view it here

import re

# input is a list of tokens (token is a number or operator)
tokens = raw_input()

# remove whitespace
tokens = re.sub('\s+', '', tokens)

# split by addition/subtraction operators
tokens = re.split('(-|\+)', tokens)

# takes in a string of numbers, *s, and /s. returns the result
def solve_term(tokens):
    tokens = re.split('(/|\*)', tokens)
    ret = float(tokens[0])
    for op, num in <FILL THIS IN>:
        # <apply the operation 'op' to the number 'num'>
    return ret

# initialize final result to the first term's value
result = solve_term(tokens[0])

# calculate the final result by adding/subtracting terms
for op, num in <FILL THIS IN>:
    result +=  solve_term(num) * (1 if op == '+' else -1)

print result

这篇关于Python创建计算器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆