我的第一个节目

2024-04-24 03:42:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我在代码学院上了很多课,但是我很沮丧,因为有些编译器拒绝返回相同结果的答案。所以,我决定离开一段时间,用我学过的计算器工具创建我自己的程序。它运行不正常。当它在菜单上时,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"

目前还没有“如果出错”的代码,但我坚持要让它正常工作。所有的功能都起作用。只是无法让选择发挥作用。在


Tags: toyouinputrawdefwhatlikeint
3条回答

除了其他人已经指出的事情,我将离开这里,我个人将如何执行它。这当然不是最好的方法,但我希望它能给你一些想法(你要么接受要么拒绝),以及你可以进一步研究的要点,以便扩展你的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))

int(rawinput())将返回一个integer,它不是==到{}这样的字符串。从它们中删除int(),它应该可以工作了。在

改变

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

^{pr2}$

数字不能用引号引起来,只能用文字字符串。
建议,您可以只获取一次输入,然后执行if/elif/else条件测试,例如:

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

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

相关问题 更多 >