How can I convert a string to an int in Python?

前端 未结 8 1354
北恋
北恋 2020-11-27 21:35

The output I\'m getting for my little example app is the following:

Welcome to the Calculator!
Please choose what you\'d like to do:
0: Addition
1: Subtracti         


        
相关标签:
8条回答
  • 2020-11-27 22:14

    easy!

        if option == str(1):
            numberA = int(raw_input("enter first number. "))
            numberB= int(raw_input("enter second number. "))
            print " "
            print addition(numberA, numberB)
         etc etc etc
    
    0 讨论(0)
  • 2020-11-27 22:17

    Since you're writing a calculator that would presumably also accept floats (1.5, 0.03), a more robust way would be to use this simple helper function:

    def convertStr(s):
        """Convert string to either int or float."""
        try:
            ret = int(s)
        except ValueError:
            #Try float.
            ret = float(s)
        return ret
    

    That way if the int conversion doesn't work, you'll get a float returned.

    Edit: Your division function might also result in some sad faces if you aren't fully aware of how python 2.x handles integer division.

    In short, if you want 10/2 to equal 2.5 and not 2, you'll need to do from __future__ import division or cast one or both of the arguments to float, like so:

    def division(a, b):
        return float(a) / float(b)
    
    0 讨论(0)
  • 2020-11-27 22:19

    While calling your sub functions from your main functions you can convert the variables into int and then call. Please refer the below code:

    import sys
    
    print("Welcome to Calculator\n")
    print("Please find the options:\n" + "1. Addition\n" + "2. Subtraction\n" + 
    "3. Multiplication\n" + "4. Division\n" + "5. Exponential\n" + "6. Quit\n")
    
    def calculator():
        choice = input("Enter choice\n")
    
        if int(choice) == 1:
            a = input("Enter first number\n")
            b = input("Enter second number\n")
            add(int(a), int(b))
    
        if int(choice) == 2:
            a = input("Enter first number\n")
            b = input("Enter second number\n")
            diff(int(a), int(b))
    
        if int(choice) == 3:
            a = input("Enter first number\n")
            b = input("Enter second number\n")
            mult(int(a), int(b))
    
        if int(choice) == 4:
            a = input("Enter first number\n")
            b = input("Enter second number\n")
            div(float(a), float(b))
    
        if int(choice) == 5:
            a = input("Enter the base number\n")
            b = input("Enter the exponential\n")
            exp(int(a), int(b))
    
        if int(choice) == 6:
            print("Bye")
            sys.exit(0)
    
    
    
    def add(a, b):
        c = a+b
        print("Sum of {} and {} is {}".format(a, b, c))
    
    def diff(a,b):
        c = a-b
        print("Difference between {} and {} is {}".format(a, b, c))
    
    def mult(a, b):
        c = a*b
        print("The Product of {} and {} is {}".format(a, b, c))
    
    def div(a, b):
        c = a/b
        print("The Quotient of {} and {} is {}".format(a, b, c))
    
    def exp(a, b):
        c = a**b
        print("The result of {} to the power of {} is {}".format(a, b, c))
    
    calculator()
    

    Here what I did is I called each of the function while converting the parameters inputted to int. I hope this has been helpful.

    In your case it could be changed like this:

     if choice == "0":
            numberA = raw_input("Enter your first number: ")
            numberB = raw_input("Enter your second number: ")
            print "Your result is:"
            print addition(int(numberA), int(numberB))
    
    0 讨论(0)
  • 2020-11-27 22:19

    def addition(a, b): return a + b

    def subtraction(a, b): return a - b

    def multiplication(a, b): return a * b

    def division(a, b): return a / b

    keepProgramRunning = True

    print "Welcome to the Calculator!"

    while keepProgramRunning:
    print "Please choose what you'd like to do:"

    print "0: Addition"
    print "1: Subtraction"
    print "2: Multiplication"
    print "3: Division"
    print "4: Quit Application"
    
    
    
    #Capture the menu choice.
    choice = raw_input()    
    
    if choice == "0":
        numberA = input("Enter your first number: ")
        numberB = input("Enter your second number: ")
        print "Your result is: " + str(addition(numberA, numberB)) + "\n"
    elif choice == "1":
        numberA = input("Enter your first number: ")
        numberB = input("Enter your second number: ")
        print "Your result is: " + str(subtraction(numberA, numberB)) + "\n"
    elif choice == "2":
        numberA = input("Enter your first number: ")
        numberB = input("Enter your second number: ")
        print "Your result is: " + str(multiplication(numberA, numberB)) + "\n"
    elif choice == "3":
        numberA = input("Enter your first number: ")
        numberB = input("Enter your second number: ")
        print "Your result is: " + str(division(numberA, numberB)) + "\n"
    elif choice == "4":
        print "Bye!"
        keepProgramRunning = False
    else:
        print "Please choose a valid option."
        print "\n"
    
    0 讨论(0)
  • 2020-11-27 22:21
    >>> a = "123"
    >>> int(a)
    123
    

    Here's some freebie code:

    def getTwoNumbers():
        numberA = raw_input("Enter your first number: ")
        numberB = raw_input("Enter your second number: ")
        return int(numberA), int(numberB)
    
    0 讨论(0)
  • 2020-11-27 22:21
    def addition(a, b): return a + b
    
    def subtraction(a, b): return a - b
    
    def multiplication(a, b): return a * b
    
    def division(a, b): return a / b
    
    keepProgramRunning = True
    
    print "Welcome to the Calculator!"
    
    while keepProgramRunning:
     print "Please choose what you'd like to do:"
    
    0 讨论(0)
提交回复
热议问题