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

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

问题描述

我对 python 相当陌生.

I am fairly new to python.

我被要求仅使用字符串命令、int/string/float 之间的转换等(如果需要)创建一个计算器,并且需要使用函数.也可以使用 while 和 for 循环.

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 形式的输入,其中 x y z 是任何正数或负数.其中/"也可以用加法乘法和减法代替.并且在操作数和运算符之间可以存在任意数量的空格.这是我目前的想法.

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天全站免登陆