【问题标题】:are all values put into a variable treated as strings?放入变量中的所有值都被视为字符串吗?
【发布时间】:2012-12-15 04:02:09
【问题描述】:

在python中,如果我给x(x = 1)赋值一个1,那么那个1是否每次都被视为一个字符串?

我不明白为什么不将数字视为数字而必须将其转换为 识别用于数学运算的整数。不断改变似乎很麻烦 来回变量值。

谢谢

第 2 部分: 圆面积程序的一些代码:

def chooseDim ( ):
    **choice = input ('Do you need to find radius or area? ')
    if choice == 'A' or 'a':
        area = 0                [This part of the prog is the culprit, specifically the
        area = int(area)         "choice" variable prior to the If conditional. The If 
        areaSol ( )**           keeps reading the condition as True no matter what value
                                "choice" is. It has to do w/the "or" condition. When I let
                                "choice" be one value, it's fine, but if I code
                                "if choice = 'A' or 'a'"  the prog evals the If as True                  
                                every time. Am I not coding the "or" statement right?]

    elif choice == 'R' or 'r':

        radSol ( )

    else:
        print ('Please enter either A/a or R/r.')
        chooseDim ( )

【问题讨论】:

    标签: string variables python-3.x int


    【解决方案1】:

    没有。所有值都不会转换为字符串。 Python 虽然是“动态类型”的,因为变量在其生命周期内可能包含不同类型的值,但通常对操作非常严格(例如,您不能只将数字连接到字符串上)。如果您将数字分配给变量,则该值将是一个数字。

    如果您对某段代码有疑问,可以发布它吗?

    【讨论】:

      【解决方案2】:

      不,你说的不正确。 Python 是动态类型的,这意味着虽然变量 有类型,但它的值确实有类型。您可以通过使用type 函数轻松检查该值的类型:

      >>> x = 1
      >>> type(x)
      <class 'int'>
      >>> x = '1' # storing a different value in x
      >>> type(x)
      <class 'str'>
      >>> type(1) # type() works on the value, so this works too
      <class 'int'>
      >>> type('1')
      <class 'str'>
      

      然而,Python 也是强类型的,这意味着不会进行动态类型转换。这就是在执行算术运算时必须将字符串显式转换为数字的原因:

      >>> 1 + '1' # int does not allow addition of a string
      Traceback (most recent call last):
        File "<pyshell#7>", line 1, in <module>
          1 + '1'
      TypeError: unsupported operand type(s) for +: 'int' and 'str'
      
      >>> '1' + 1 # on the other hand, int cannot be converted to str implicitely
      Traceback (most recent call last):
        File "<pyshell#6>", line 1, in <module>
          '1' + 1
      TypeError: Can't convert 'int' object to str implicitly
      

      编辑

      要响应您发布的代码,您的 if 是这样的:

      if choice == 'A' or 'a':
      

      这会检查choice == 'A'(如果choice 等于'A'),如果'a' 计算结果为True。由于'a' 是一个非空字符串,它总是会计算为真,所以条件总是真。你要写的是这样的:

      if choice == 'A' or choice == 'a':
      

      【讨论】:

      • 非常有帮助;在长时间中断编程(COBOL,JCL)后,我上周才开始使用 python; “type”命令将是最有用的
      • def chooseDim ( ): #求圆的面积/半径choice = input ('你需要求半径还是面积?') ifchoice == 'A' or 'a': area = 0 area = int(area) areaSol ( ) elif choice == 'R' or 'r': radius = 0 radius = int(radius) radSol ( ) else: print ('请输入 A/a 或 R/r .') 选择Dim ( )
      • @Fluxcapacitor 您可以将该代码添加到问题中吗?它在 cmets 中不可读(尤其是 Python 需要空格)。
      • 请见谅;我遇到了这个编辑器的问题;我把东西打出来又漂亮又整洁,然后它就嘎吱作响了;我会修改它
      • 我将把这个问题作为一个新帖子提交。寻找它。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-25
      • 2019-06-15
      • 1970-01-01
      • 2011-08-24
      • 1970-01-01
      相关资源
      最近更新 更多