计算器功能什么都不输出 [英] Calculator function outputs nothing

查看:88
本文介绍了计算器功能什么都不输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用以下UI设计计算器:

I need to design a calculator with the following UI:

Welcome to Calculator!
1  Addition
2  Subtraction
3  Multiplication
4  Division
Which operation are you going to use?: 1
How many numbers are you going to use?: 2
Please enter the number: 3
Please enter the number: 1
The answer is 4.

这是我到目前为止的内容:

This is what I have so far:

print("Welcome to Calculator!")

class Calculator:
    def addition(self,x,y):
        added = x + y
        return sum
    def subtraction(self,x,y):
        diff = x - y
        return diff
    def multiplication(self,x,y):
        prod = x * y
        return prod
    def division(self,x,y):
        quo = x / y
        return quo


calculator = Calculator()

print("1 \tAddition")
print("2 \tSubtraction")
print("3 \tMultiplication")
print("4 \tDivision")
operations = int(input("What operation would you like to use?:  "))

x = int(input("How many numbers would you like to use?:  "))

if operations == 1:
    a = 0
    sum = 0
    while a < x:
        number = int(input("Please enter number here:  "))
        a += 1
        sum = calculator.addition(number,sum)

我非常需要帮助! Python 3中有关计算器的所有教程都比这简单得多(因为它只需要2个数字,然后简单地打印出答案)。

I seriously need some help! All the tutorials on calculators in Python 3 are far more simple than this (as in it only take 2 numbers and then simply prints out the answer).

我需要帮助来获取使我工作的Calculator类中的功能。当我尝试运行到目前为止的内容时,它可以让我输入我的数字和其他信息,但随后就结束了。它不会运行该操作等。我知道到目前为止我只有加法,但是如果有人可以帮我弄清楚加法,我想我可以做剩下的。

I need help getting the functions in the Calculator class I made to work. When I try running what I have so far, it lets me input my numbers and whatnot, but then it just ends. It doesn't run the operation or whatever. I am aware I only have addition so far, but if someone can just help me figure out addition, I think I can do the rest.

推荐答案

原样的代码不起作用。在加成函数中,返回变量 sum ,该变量将与 sum 函数。

The code as it is does not work. In the addition function you return the variable sum which will conflict with the build in sum function.

因此只需返回添加的内容,通常避免使用 sum sum _ 一样:

So just return the added and in general avoid the sum, use something like sum_ :

对我来说这很好:

print("Welcome to Calculator!")

class Calculator:
    def addition(self,x,y):
        added = x + y
        return added
    def subtraction(self,x,y):
        diff = x - y
        return diff
    def multiplication(self,x,y):
        prod = x * y
        return prod
    def division(self,x,y):
        quo = x / y
        return quo

calculator = Calculator()

print("1 \tAddition")
print("2 \tSubtraction")
print("3 \tMultiplication")
print("4 \tDivision")
operations = int(input("What operation would you like to use?:  "))

x = int(input("How many numbers would you like to use?:  "))

if operations == 1:
    a = 0
    sum_ = 0
    while a < x:
        number = int(input("Please enter number here:  "))
        a += 1
        sum_ = calculator.addition(number,sum_)

    print(sum_)

运行:

$ python s.py
Welcome to Calculator!
1   Addition
2   Subtraction
3   Multiplication
4   Division
What operation would you like to use?:  1
How many numbers would you like to use?:  2
Please enter number here:  45
Please enter number here:  45
90

这篇关于计算器功能什么都不输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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