-1

我正在制作一个计算器程序,它具有平方根功能,但首先,您需要输入“s”才能访问它,我想制作它以便用户可以输入“S”或“s”并拥有计算机仍然可以识别它并调出平方根选项,但是如果我添加 s.upper() 和 s 变量,它可以工作但不是预期的代码是:

import math

def calculator():
    while True:
        s = "s"
        intro = input('Hello! Please type * for multiplication, / for division, + for addition, - for subtraction, ** for exponents, and "s" for square root \n')
        if intro not in ["*", "/", "+", "-", "**", s.upper(), s]:
            print ("that wasnt an option!")
            continue
        if intro != s:
           num1 = int(input("Whats your first number \n"))
           num2 = int(input("Whats your second number \n"))
        if intro != s.upper():
           num1 = int(input("Whats your first number \n"))
           num2 = int(input("Whats your second number \n"))  
        if intro == "*":
            print(num1 * num2)
            break
        elif intro == "/":
            print(num1/num2)
            break
        elif intro == "+":
            print(num1 + num2)
            break
        elif intro == "-":
            print(num1 - num2)
            break
        elif intro == "**":
            print(num1 ** num2)
            break
        elif intro == s.upper():
            num_sqr = int(input("What number do you wanna find the square root of \n"))
            print(math.sqrt(num_sqr))
            break
        elif intro == s:
            num_sqr = int(input("What number do you wanna find the square root of \n"))
            print(math.sqrt(num_sqr))
            break
            
            
calculator()

每当用户键入 s 时,它都会忽略变量 num1 和 num2 ,因此它可以运行 num_sqr 变量,因此输出为:

s (or S)

Whats your first number 
2

Whats your second number 
4

What number do you wanna find the square root of 
24
4.898979485566356

而不是:

s (or S)

What number do you wanna find the square root of 
24
4.898979485566356

为什么会这样,我该如何解决?

4

5 回答 5

0

代码的问题是您实际上并没有格式化输入。您只需要upper()输入,并用“S”检查所有条件。这也将缩短您的代码。

import math


def calculator():
    while True:
        intro = input(
            'Hello! Please type * for multiplication, / for division, + for addition, - for subtraction, ** for exponents, and "s" for square root \n').upper()
        if intro not in ["*", "/", "+", "-", "**", "S"]:
            print("that wasnt an option!")
            calculator()
        if intro != "S":
            num1 = int(input("Whats your first number \n"))
            num2 = int(input("Whats your second number \n"))
        if intro == "*":
            print(num1 * num2)
            break
        elif intro == "/":
            print(num1/num2)
            break
        elif intro == "+":
            print(num1 + num2)
            break
        elif intro == "-":
            print(num1 - num2)
            break
        elif intro == "**":
            print(num1 ** num2)
            break
        elif intro == "S":
            num_sqr = int(
                input("What number do you wanna find the square root of \n"))
            print(math.sqrt(num_sqr))
            break


calculator()
于 2020-07-15T01:52:14.180 回答
0

您可以在询问第一个或第二个数字之前运行“s”或“S”命令,而不是检查命令是否为 s 然后等到最后执行平方根命令。这段代码会做`

import math

def calculator():
    while True:
        s = "s"
        intro = input('Hello! Please type * for multiplication, / for division, + for addition, - for subtraction, ** for exponents, and "s" for square root \n')
        if intro not in ["*", "/", "+", "-", "**", s.upper(), s]:
            print ("that wasnt an option!")
            continue
        if intro == s:
          num_sqr = int(input("What number do you wanna find the square root of \n"))
          print(math.sqrt(num_sqr))
          break

        if intro == s.upper():
            num_sqr = int(input("What number do you wanna find the square root of \n"))
            print(math.sqrt(num_sqr))
            break


        if intro != s:
           num1 = int(input("Whats your first number \n"))
           num2 = int(input("Whats your second number \n"))
        if intro != s.upper():
           num1 = int(input("Whats your first number \n"))
           num2 = int(input("Whats your second number \n"))  
        if intro == "*":
            print(num1 * num2)
            break
        elif intro == "/":
            print(num1/num2)
            break
        elif intro == "+":
            print(num1 + num2)
            break
        elif intro == "-":
            print(num1 - num2)
            break
        elif intro == "**":
            print(num1 ** num2)
            break
        
            
            
calculator()
于 2020-07-15T01:28:25.387 回答
0

您使用两个不同的条件创建了两个 if 语句,即使您输入“s”或“S”,其中一个 if 语句将始终评估为 True。

    # ONE OF THIS IF STATEMENT IS GOING TO BE TRUE ALL TIME WHEN ENTER "S" or "s"
    if intro != s:
       num1 = int(input("Whats your first number \n"))
       num2 = int(input("Whats your second number \n"))
    if intro != s.upper():     
       num1 = int(input("Whats your first number \n"))
       num2 = int(input("Whats your second number \n")) 

您可以通过仅在一个 if 语句中评估这两个条件来轻松解决此问题

    if intro != s and intro != s.upper():               #<-- if not "s" or "S" 
       num1 = int(input("Whats your first number \n"))
       num2 = int(input("Whats your second number \n"))
于 2020-07-15T02:31:22.417 回答
0

尝试改变这个:

intro = input('Hello! Please type * for multiplication, / for division, + for addition, - for subtraction, ** for exponents, and "s" for square root \n')

进入这个:

intro = input('Hello! Please type * for multiplication, / for division, + for addition, - for subtraction, ** for exponents, and "s" for square root \n').lower()
于 2020-07-15T03:44:03.907 回答
0

继续并在您的介绍字符串上调用 .capitalize() 字符串方法;它将确保字符串字母的格式正确,而不会弄乱运算符的格式。

import math

def calculator():
    while True:
        intro = input('Hello! Please type * for multiplication, / for division, + for addition, - for subtraction, ** for exponents, and "s" for square root \n')
        intro = intro.capitalize()
        if intro not in ["*", "/", "+", "-", "**", "S"]:
            print ("that wasnt an option!")
            continue
        if intro != "S":
           num1 = int(input("Whats your first number \n"))
           num2 = int(input("Whats your second number \n"))
        if intro == "*":
            print(num1 * num2)
            break
        elif intro == "/":
            print(num1/num2)
            break
        elif intro == "+":
            print(num1 + num2)
            break
        elif intro == "-":
            print(num1 - num2)
            break
        elif intro == "**":
            print(num1 ** num2)
            break
        else:
            num_sqr = int(input("What number do you wanna find the square root of \n"))
            print(math.sqrt(num_sqr))
            break
            
            
calculator()


也只是为了好玩,因为这是评估字符串表达式,Python 实际上为我们提供了一个方便的函数,用于使用该eval()函数对字符串对象进行计算。所以我们也可以这样做:

def calculator():
    while True:
        intro = input('Hello! Please type * for multiplication, / for division, + for addition, - for subtraction, ** for exponents, and "s" for square root \n')
        intro = intro.capitalize()
        if intro not in ["*", "/", "+", "-", "**", "S"]:
            print ("that wasnt an option!")
            continue
        if intro != "S":
            num1 = input("Whats your first number \n")
            num2 = input("Whats your second number \n")
            print(
                eval(num1+intro+num2)
            )
            break
        else:
            num_sqr = int(input("What number do you wanna find the square root of \n"))
            print(math.sqrt(num_sqr))
            break

于 2020-07-15T01:19:31.750 回答