我的第一个节目

时间:2013-12-09 05:36:57

标签: python if-statement calculator

我一直在为Code Academy做很多课程,但是我感到很沮丧,因为有些编译器会拒绝返回相同结果的答案。所以,我决定突破一点,用我学到的工具创建自己的程序,这是一个计算器。它无法正常运行。当它在菜单中时,if / elif / else语句不起作用。该程序似乎忽略了输入。所以,这是我的第一个程序代码......

import math
user_choice=(">>")
def add():
    print "What two numbers would you like to add?"
    a= int(raw_input(user_choice))                     
    b= int(raw_input(user_choice))
    c= a + b
    print c
def sub():
    print "What two numbers would you like to subtract?"
    a=int(raw_input(user_choice))
    b=int(raw_input(user_choice))
    c=a-b
    print c
def mult():
    print "What two numbers would you like to multiply?"
    a=int(raw_input(user_choice))
    b=int(raw_input(user_choice))
    c= a*b
    print c
def div():
    print "What two numbers would you like to divide?"
    a=int(raw_input(user_choice))
    b=int(raw_input(user_choice))
    c=a/b
    print c
def exp():
    print "What number would you like to power?"
    a=int(raw_input(user_choice))
    print "By what number would you like it to be powered to?"
    b=int(raw_input(user_choice))
    c= math.pow(a,b)
    print  c
def square():
    print "What number would you like to square root?"
    a=int(raw_input(user_choice))
    b=math.sqrt(a)
    print b

print "+---------------------------+"   
print "|   Welcome to my basic     |"
print "|    calculator!         |"
print "|                           |"
print "|What would you like to do? |"
print "|                |"
print "|1: Addition         |"
print "|2: Subtraction          |"
print "|3: Multiplication       |"
print "|4: Division         |"
print "|5: Exponents            |"
print "|6: Square Root          |"
print "|7: Quit         |"
print "|                           |"
print "+---------------------------+"

if int(raw_input(user_choice)) ==  "1":
     add()

elif int(raw_input(user_choice)) == "2":
     sub()

elif int(raw_input(user_choice)) == "3":
     mult()

elif int(raw_input(user_choice)) == "4":
     div()

elif int(raw_input(user_choice)) == "5":
     exp()

elif int(raw_input(user_choice)) == "6":
     square()

elif int(raw_input(user_choice)) == "7":
     exit()

else:
    print "Sorry, I didn't understand your entry.Try entering a value 1-7"

还没有“if error”代码,但我坚持要让它运行起来。所有功能都有效。只是无法让选项工作。

3 个答案:

答案 0 :(得分:4)

int(rawinput())将返回integer,该==不会"1"int()之类的字符串。从他们中删除{{1}},它应该可以。

答案 1 :(得分:1)

更改

if int(raw_input(user_choice)) ==  "1":

if int(raw_input(user_choice)) ==  1:

不应引用数字,只应使用文字字符串 一个建议,您只需输入一次输入,然后执行if / elif / else条件测试,例如:

option = int(raw_input(user_choice))
print "You choose %d" % option

if option == 1:
    add()
elif option == 2:
    sub()
......

答案 2 :(得分:1)

除了其他人指出的事情,我将离开这里,我个人将如何实施它。这肯定不是最好的方法,但我希望它可能会给你一些想法(你要么采用也要拒绝)以及一些可以进一步调查以扩展你的python技能的要点:

class Operation:
    def __init__(self, f, argc):
        #f is the function to use, argc the number of arguments it takes
        self.f = f
        self.argc = argc

    def __call__(self):
        #read in the args
        args = [int(input('>> ')) for _ in range (self.argc)]
        #print out the result of the function passed in the ctor
        print(self.f(*args))

#your operations keyed to the options of your menu
operations = {'1': Operation(lambda a, b: a + b, 2),
              '2': Operation(lambda a, b: a - b, 2),
              '3': Operation(lambda a, b: a * b, 2),
              '4': Operation(lambda a, b: a / b, 2),
              '5': Operation(lambda a, b: a ** b, 2),
              '6': Operation(lambda a: a ** .5, 1)}

while True:
    print('''
1: Addition
2: Subtraction
3: Multiplication
4: Division
5: Exponentiation
6: Square root
7: Quit''')
    choice = input('Your choice: ') #raw_input for py2
    if choice == '7': break
    try: operations[choice]()
    #KeyError means choice is not in the dict
    except KeyError: print('Unkown option')
    #Something else went wrong, division by zero, etc
    except Exception as e: print('Something went horribly wrong: {}'.format(e))