尝试在Python中使用字符串制作计算器

raogr8fs  于 2023-08-08  发布在  Python
关注(0)|答案(3)|浏览(95)

我试图使一个python程序,用户可以输入“1233+1333”或“23566*5456”,然后按Enter键,程序会自动计算值,如果用户输入“54432a+2432b”,它应该显示一个错误,即输入无效

def add(num1, num2):
    return num1 + num2

def subtract(num1, num2):
    return num1 - num2

def multiply(num1, num2):
    return num1 * num2

def divide(num1, num2):
    return num1 / num2

print("Please select operation -\n" \
        "1. Add\n" \
        "2. Subtract\n" \
        "3. Multiply\n" \
        "4. Divide\n")
 
select = int(input("Select operations form 1, 2, 3, 4 :"))
 
number_1 = int(input("Enter first number: "))
number_2 = int(input("Enter second number: "))
 
if select == 1:
    print(number_1, "+", number_2, "=",
                    add(number_1, number_2))
 
elif select == 2:
    print(number_1, "-", number_2, "=",
                    subtract(number_1, number_2))
 
elif select == 3:
    print(number_1, "*", number_2, "=",
                    multiply(number_1, number_2))
 
elif select == 4:
    print(number_1, "/", number_2, "=",
                    divide(number_1, number_2))
else:
    print("Invalid input")

字符串

pvabu6sv

pvabu6sv1#

你的代码做得很好,函数的使用方法也很好,关于这个问题,我建议在代码的这个特定部分做一些修改

number_1 = int(input("Enter first number: "))
number_2 = int(input("Enter second number: "))

字符串
你正在将输入转换为整数,如果用户输入了一个非法的输入,如“54432a”,代码将中断并显示一个值错误。有多种方法可以实现这一点,最好的方法是使用assert语句,
但我建议的是一种简单的方法来删除类型转换,而是检查用户输入的值的类型。并设置一个条件来检查它们是否是数字,因为你正在使用input函数将输入转换为字符串,你可以在下一行将它们转换为整数,这是我的建议,尽管有更多的最佳解决方案。干杯。

def add(num1, num2):
    return num1 + num2

def subtract(num1, num2):
    return num1 - num2

def multiply(num1, num2):
    return num1 * num2

def divide(num1, num2):
    return num1 / num2
    
print("Please select operation -\n" \
        "1. Add\n" \
        "2. Subtract\n" \
        "3. Multiply\n" \
        "4. Divide\n")
 
select = int(input("Select operations form 1, 2, 3, 4 :"))
 
number_1 = input("Enter first number: ")
number_2 = input("Enter second number: ")

if number_1.isnumeric() == True and number_2.isnumeric() == True: 
    number_1, number_2 = int(number_1), int(number_2)
    if select == 1:
        print(number_1, "+", number_2, "=",
                        add(number_1, number_2))
    
    elif select == 2:
        print(number_1, "-", number_2, "=",
                        subtract(number_1, number_2))
    
    elif select == 3:
        print(number_1, "*", number_2, "=",
                        multiply(number_1, number_2))
    
    elif select == 4:
        print(number_1, "/", number_2, "=",
                        divide(number_1, number_2))
else:
    print("Invalid input")

rn0zuynd

rn0zuynd2#

这里有一种方法可以完成您的请求,使用正则表达式。这只处理具有两个整数操作数的表达式。

import re

pat = r'(\d+)([-+*/])(\d+)'

while True:
    expr = input('Enter expression:')
    parts = re.match(pat,expr)
    if not parts:
        print( "Your input didn't match my pattern.")
        continue
    n1 = int(parts[1])
    n2 = int(parts[3])
    op = parts[2]
    if op == '+':
        print(n1+n2)
    elif op == '-':
        print(n1-n2)
    elif op == '*':
        print(n1*n2)
    elif op == '/':
        print(n1/n2)
    break

字符串

kgsdhlau

kgsdhlau3#

一旦input不是int,它就会直接抛出一个错误,因为在Python中,if statement先执行,然后是else。相反,您可以使用类似exception handling这样的代码

代码修正

def add(num1, num2):
    return num1 + num2

def subtract(num1, num2):
    return num1 - num2

def multiply(num1, num2):
    return num1 * num2

def divide(num1, num2):
    return num1 / num2

print("Please select operation -\n" \
        "1. Add\n" \
        "2. Subtract\n" \
        "3. Multiply\n" \
        "4. Divide\n")
 
while 1:
  
    select = int(input("Select operations form 1, 2, 3, 4 :"))

    try:
        number_1 = int(input("Enter first number: "))
    except ValueError:
       print("Invalid input")
       break

    try:
        number_2 = int(input("Enter second number: "))
    except ValueError:
       print("Invalid input")
       break
     
    if select == 1:
        print(number_1, "+", number_2, "=",
                        add(number_1, number_2))
     
    elif select == 2:
        print(number_1, "-", number_2, "=",
                        subtract(number_1, number_2))
     
    elif select == 3:
        print(number_1, "*", number_2, "=",
                        multiply(number_1, number_2))
     
    elif select == 4:
        print(number_1, "/", number_2, "=",
                        divide(number_1, number_2))
    else:
        print("Invalid input")

字符串

相关问题