如何在Python中将字符串转换为int? [英] How can I convert a string to an int in Python?

查看:153
本文介绍了如何在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

这是因为addition()方法将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 "\n"


推荐答案

自你正在写一个计算器,可能也接受浮点数( 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

如果是这样的话转换不起作用,你会得到一个浮动返回。

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

编辑:你的除法函数也可能导致如果您不完全了解 python 2.x如何处理整数除法

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

简而言之,如果你想 10/2 等于 2.5 2 ,你需要从__future__进口部门 或者将一个或两个参数转换为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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆