用python制作计算器

时间:2019-01-24 20:44:37

标签: python input python-2.x

因此,由于我刚刚开始学习,因此我尝试使用这段代码在python中创建计算器。问题是,即使我确实输入了有效的选项,它总是说我输入了无效的选项,并通过了所有if-else语句。我做错了什么?

    #!/usr/bin/env python

def add():
    return float(input ("Enter a number: ")) + float(input ("Enter another number: "))

def subt():
    return float(input ("Enter a number: ")) - float(input ("Enter another number: "))

def mult():
    return float(input ("Enter a number: ")) * float(input ("Enter another number: "))

def power():
    return float(input ("Enter a number: ")) ** float(input ("Enter another number: "))

def division():
    return float (input ("Enter a number: ")) / float (input ("Enter another number: "))

s = input("Add, Subtract, Multiply, Divide or Power two Numbers: ")
if s == "add":
    print(add ())
elif s == "subtract":
    print(subt ())
elif s == "multiply":
    print(mult ())
elif s == "power":
    print(power ())
elif s == "division":
    print(division ())
else:
    print ("Enter a valid option")

2 个答案:

答案 0 :(得分:1)

如果听起来您正在使用Python2。在这种情况下,请使用raw_input而不是input,否则它将尝试通过用户字符串输入来定位变量/函数,并将对象名放在s中。其中,raw_input作为用户输入作为字符串并放置字符串s

这是两者之间区别的简短答案。

#!/usr/bin/env python

def add():
    return float(input ("Enter a number: ")) + float(input ("Enter another number: "))

def subt():
    return float(input ("Enter a number: ")) - float(input ("Enter another number: "))

def mult():
    return float(input ("Enter a number: ")) * float(input ("Enter another number: "))

def power():
    return float(input ("Enter a number: ")) ** float(input ("Enter another number: "))

def division():
    return float (input ("Enter a number: ")) / float (input ("Enter another number: "))

s = raw_input("Add, Subtract, Multiply, Divide or Power two Numbers: ")
if s == "add":
    print(add())
elif s == "subtract":
    print(subt())
elif s == "multiply":
    print(mult())
elif s == "power":
    print(power())
elif s == "division":
    print(division())
else:
    print("Enter a valid option")

Python2和Python3有很多区别,指定您使用的哪个有很大帮助。输入python解释器时,或者在执行python --version时,它都会在终端的顶部说出来。

答案 1 :(得分:0)

from tkinter import*

def btnClick(numbers): 
    global operator 
    operator = operator + str(numbers) 
    text_Input.set(operator)

def btnClearDisplay(): 
    global operator 
    operator = "" text_Input.set("")

def btnEqualsInput(): 
    global operator 
    sumup = str(eval(operator)) 
    text_Input.set(sumup) 
    operator=""

cal = Tk() 
cal.title("Suhail calculator") 
operator = "" 
text_Input = StringVar() 
txtDisplay = Entry(cal, font=('arial', 20, 'bold'), textvariable=text_Input, bd=30, insertwidth=4, bg="powder blue", justify='right').grid(columnspan=4)

btn7 = Button(cal, padx=16, bd=8, fg="black", font=('arial', 20, 'bold'), text="7", bg="powder blue", command=lambda:btnClick(7)).grid(row=1, column=0)

btn8 = Button(cal, padx=16, bd=8, fg="black", font=('arial', 20, 'bold'), text="8", bg="powder blue", command=lambda:btnClick(8)).grid(row=1, column=1)

btn9 = Button(cal, padx=16, bd=8, fg="black", font=('arial', 20, 'bold'), text="9", bg="powder blue", command=lambda:btnClick(9)).grid(row=1, column=2)

addition = Button(cal, padx=16, bd=8, fg="black", font=('arial',20,'bold'), text="+", bg="powder blue", command=lambda:btnClick("+")).grid(row=1, column=3) #================================================================================ 
btn4 = Button(cal, padx=16, bd=8, fg="black", font=('arial', 20, 'bold'), text="4", bg="powder blue", command=lambda:btnClick(4)).grid(row=2, column=0)

btn5 = Button(cal, padx=16, bd=8, fg="black",font=('arial', 20, 'bold'), text="5", bg="powder blue", command=lambda:btnClick(5)).grid(row=2, column=1)

btn6 = Button(cal, padx=16, bd=8, fg="black", font=('arial', 20, 'bold'), text="6", bg="powder blue", command=lambda:btnClick(6)).grid(row=2, column=2)

subtraction = Button(cal, padx=16, bd=8, fg="black", font=('arial', 20, 'bold'), text="-", bg="powder blue", command=lambda:btnClick("-")).grid(row=2, column=3) #================================================================================= 
btn1 = Button(cal, padx=16, bd=8, fg="black", font=('arial', 20, 'bold'), text="1", bg="powder blue", command=lambda:btnClick(1)).grid(row=3, column=0)

btn2 = Button(cal, padx=16, bd=8, fg="black", font=('arial', 20, 'bold'), text="2", bg="powder blue", command=lambda:btnClick(2)).grid(row=3, column=1)

btn3 = Button(cal, padx=16, bd=8, fg="black", font=('arial', 20, 'bold'), text="3", bg="powder blue", command=lambda:btnClick(3)).grid(row=3, column=2)

multiply = Button(cal, padx=16, bd=8, fg="black", font=('arial', 20, 'bold'), text="", bg="powder blue", command=lambda:btnClick("")).grid(row=3, column=3)

#=================================================================================== 
btn0 = Button(cal, padx=16, pady=16, bd=8, fg="black", font=('arial', 20, 'bold'), text="0", bg="powder blue", command=lambda:btnClick(0)).grid(row=4, column=0)

btnClear = Button(cal, padx=16, pady=16, bd=8, fg="black", font=('arial', 20, 'bold'), text="C", bg="powder blue", command=btnClearDisplay).grid(row=4, column=1)

btnEqual = Button(cal, padx=16, pady=16, bd=8, fg="black", font=('arial', 20, 'bold'), text="=", bg="powder blue", command=btnEqualsInput).grid(row=4, column=2)

Division = Button(cal, padx=16, pady=16, bd=8, fg="black", font=('arial', 20, 'bold'), text="/", bg="powder blue", command=lambda:btnClick("/")).grid(row=4, column=3) #==================================================================================

cal.mainloop()