【问题标题】:How to allow user to enter no number for an operation in a calculator如何允许用户在计算器中不输入数字进行运算
【发布时间】:2021-05-04 05:10:04
【问题描述】:

这是我在 Python 中的第三个项目。我正在做一个计算器,我有所有的操作等等,工作得很好。但是,我想让用户不必在两个操作(即欧拉数和 Pi)上输入第二个数字,并且计算器仍然可以工作。例如,如果我想将一个数字乘以 pi,现在我需要输入“1”作为第二个数字。我想消除这种需求。 非常感谢您提供的任何帮助。

import math


def welcome():
    print('''
Welcome to Calculator
''')


# Define our function
welcome()


def calculate():
    operation = input('''
Please type in the math operation you would like to complete:
+ for addition
- for subtraction
* for multiplication
/ for division
** for power
% for modulo
pi to multiply by Pi 
e to multiply by Euler's Number 
''')

    number_1 = float(input('Please enter the first number: '))
    number_2 = float(input('Please enter the second number: '))
    if operation == 'e':
        print('{} * e = '.format(number_1, math.e))
        print(number_1 * math.e)
    elif operation == 'pi':
        print('{} * Pi = '.format(number_1, math.pi))
        print(number_1 * math.pi)
    elif operation == '+':
        print('{} + {} = '.format(number_1, number_2))
        print(number_1 + number_2)
    elif operation == '-':
        print('{} - {} = '.format(number_1, number_2))
        print(number_1 - number_2)
    elif operation == '*':
        print('{} * {} = '.format(number_1, number_2))
        print(number_1 * number_2)
    elif operation == '/':
        print('{} / {} = '.format(number_1, number_2))
        print(number_1 / number_2)
    elif operation == '**':
        print('{} ** {} ='.format(number_1, number_2))
        print(number_1 ** number_2)
    elif operation == '%':
        print('{} % {} ='.format(number_1, number_2))
        print(number_1 % number_2)
    else:
        print('You have not typed a invalid operator, please run the program again.')

    # Add again() function to calculate() function
    again()


def again():
    calc_again = input('''
Do you want to calculate again?
Please type Y for YES or N for NO.
''')

    if calc_again.upper() == 'Y':
        calculate()
    elif calc_again.upper() == 'N':
        print("Thank you for using the calculator. I'll be here whenever you need me.")
    else:
        again()


# Call calculate() outside of the function
calculate()

【问题讨论】:

  • 创建一个只有在用户在 ['*','+','-' 等中输入字符时才会中断的 while 循环]

标签: python python-3.x user-input calculator


【解决方案1】:

在您需要更改的特定片段上归零,这是一种方法:

...
    number_1 = float(input('Please enter a number: '))
    if operation == 'e' or operation == 'pi':
        if operation == 'e':
            print('{} * e = '.format(number_1, math.e))
            print(number_1 * math.e)
        elif operation == 'pi':
            print('{} * Pi = '.format(number_1, math.pi))
            print(number_1 * math.pi)
    else:
        number_2 = float(input('Please enter a second number: '))
        if operation == '+':
            print('{} + {} = '.format(number_1, number_2))
            print(number_1 + number_2)
        elif operation == '-':
            print('{} - {} = '.format(number_1, number_2))
            print(number_1 - number_2)
...

【讨论】:

    【解决方案2】:

    只需在上面添加一个 if 条件

    number_2 = float(input('Please enter the second number: '))
    

    这样

    if(operation != 'e' || operation != 'pi'):
       number_2 = float(input('Please enter the second number: '))
    

    【讨论】:

      【解决方案3】:

      在对 e 和 pi 进行条件检查后,将第二个数字的输入移动到所有循环中,无需对代码进行太多更改即可解决此问题。

      举个例子:

          number_1 = float(input('Please enter the first number: '))
          if operation == 'e':
              print('{} * e = '.format(number_1, math.e))
              print(number_1 * math.e)
          elif operation == 'pi':
              print('{} * Pi = '.format(number_1, math.pi))
              print(number_1 * math.pi)
          elif operation == '+':
              number_2 = float(input('Please enter the second number: '))
              print('{} + {} = '.format(number_1, number_2))
              print(number_1 + number_2)
      

      这不遵循最佳实践,但应该按照您的意愿完成。如果您想要比这更好的方法,我可以编辑此答案以包括改进以及为什么它们更好。

      【讨论】:

      • 您好,如果您能提供改进,我将不胜感激,因为我仍在学习并想了解为什么一种方法比另一种更好。谢谢。
      • rhurwitz 上面的答案做得很好。我的示例与他的示例的主要问题是我的示例您必须每次重复输入 number_2 ,而他只需执行一次。
      猜你喜欢
      • 1970-01-01
      • 2021-11-07
      • 1970-01-01
      • 2014-06-16
      • 1970-01-01
      • 1970-01-01
      • 2014-03-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多