1. <legend id='UCX4C'><style id='UCX4C'><dir id='UCX4C'><q id='UCX4C'></q></dir></style></legend><tfoot id='UCX4C'></tfoot>
    • <bdo id='UCX4C'></bdo><ul id='UCX4C'></ul>
  2. <i id='UCX4C'><tr id='UCX4C'><dt id='UCX4C'><q id='UCX4C'><span id='UCX4C'><b id='UCX4C'><form id='UCX4C'><ins id='UCX4C'></ins><ul id='UCX4C'></ul><sub id='UCX4C'></sub></form><legend id='UCX4C'></legend><bdo id='UCX4C'><pre id='UCX4C'><center id='UCX4C'></center></pre></bdo></b><th id='UCX4C'></th></span></q></dt></tr></i><div id='UCX4C'><tfoot id='UCX4C'></tfoot><dl id='UCX4C'><fieldset id='UCX4C'></fieldset></dl></div>
  3. <small id='UCX4C'></small><noframes id='UCX4C'>

      如何在 Python 中将字符串转换为 int?

      时间:2023-09-11
          <bdo id='SZj9S'></bdo><ul id='SZj9S'></ul>

          <i id='SZj9S'><tr id='SZj9S'><dt id='SZj9S'><q id='SZj9S'><span id='SZj9S'><b id='SZj9S'><form id='SZj9S'><ins id='SZj9S'></ins><ul id='SZj9S'></ul><sub id='SZj9S'></sub></form><legend id='SZj9S'></legend><bdo id='SZj9S'><pre id='SZj9S'><center id='SZj9S'></center></pre></bdo></b><th id='SZj9S'></th></span></q></dt></tr></i><div id='SZj9S'><tfoot id='SZj9S'></tfoot><dl id='SZj9S'><fieldset id='SZj9S'></fieldset></dl></div>
          <tfoot id='SZj9S'></tfoot><legend id='SZj9S'><style id='SZj9S'><dir id='SZj9S'><q id='SZj9S'></q></dir></style></legend>
          • <small id='SZj9S'></small><noframes id='SZj9S'>

                <tbody id='SZj9S'></tbody>
              • 本文介绍了如何在 Python 中将字符串转换为 int?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我的小示例应用程序的输出如下:

                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: Subtraction
                2: Multiplication
                3: Division
                4: Quit Application
                0
                Enter your first number: 1
                Enter your second number: 1
                Your result is:
                11
                

                这是因为 add() 方法将 input() 作为字符串而不是数字.如何将它们用作数字?

                This is because the addition() method is taking the input() as strings and not numbers. How can I use them as numbers?

                这是我的整个脚本:

                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 = raw_input("Enter your first number: ")
                        numberB = raw_input("Enter your second number: ")
                        print "Your result is:"
                        print addition(numberA, numberB)
                    elif choice == "1":
                        numberA = raw_input("Enter your first number: ")
                        numberB = raw_input("Enter your second number: ")
                        print "Your result is:"
                        print subtraction(numberA, numberB)
                    elif choice == "2":
                        numberA = raw_input("Enter your first number: ")
                        numberB = raw_input("Enter your second number: ")
                        print "Your result is:"
                        print multiplication(numberA, numberB)
                    elif choice == "3":
                        numberA = raw_input("Enter your first number: ")
                        numberB = raw_input("Enter your second number: ")
                        print "Your result is:"
                        print division(numberA, numberB)
                    elif choice == "4":
                        print "Bye!"
                        keepProgramRunning = False
                    else:
                        print "Please choose a valid option."
                        print "
                "
                

                推荐答案

                由于您正在编写一个可能也接受浮点数(1.5, 0.03)的计算器,因此更可靠的方法是使用这个简单的辅助函数:

                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
                

                这样,如果 int 转换不起作用,您将返回一个浮点数.

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

                如果您不完全了解 python 2.x 如何处理整数除法.

                简而言之,如果您希望 10/2 等于 2.5 2,您将需要执行 from __future__ import division 或将一个或两个参数转换为 float,如下所示:

                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)
                

                这篇关于如何在 Python 中将字符串转换为 int?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:如何将 int 转换为十六进制字符串? 下一篇:ValueError: int() 以 10 为底的无效文字:'stop'

                相关文章

                最新文章

              • <legend id='YD3bC'><style id='YD3bC'><dir id='YD3bC'><q id='YD3bC'></q></dir></style></legend>
                <tfoot id='YD3bC'></tfoot>
                1. <i id='YD3bC'><tr id='YD3bC'><dt id='YD3bC'><q id='YD3bC'><span id='YD3bC'><b id='YD3bC'><form id='YD3bC'><ins id='YD3bC'></ins><ul id='YD3bC'></ul><sub id='YD3bC'></sub></form><legend id='YD3bC'></legend><bdo id='YD3bC'><pre id='YD3bC'><center id='YD3bC'></center></pre></bdo></b><th id='YD3bC'></th></span></q></dt></tr></i><div id='YD3bC'><tfoot id='YD3bC'></tfoot><dl id='YD3bC'><fieldset id='YD3bC'></fieldset></dl></div>
                  • <bdo id='YD3bC'></bdo><ul id='YD3bC'></ul>
                2. <small id='YD3bC'></small><noframes id='YD3bC'>