AttributeError: 'function' 对象没有属性 'strip'

时间:2021-01-26 01:59:38

标签: python strip

我正在尝试从 platform.system 值中去除空白区域,以便我可以在某些 if/else 逻辑中进行比较。我收到以下错误。我错过了什么?

第 9 行,在 打印(os_name.strip()) AttributeError: 'function' 对象没有属性 'strip'

import platform

os_name = platform.system
os_name_strip = os_name.strip()

print('OS Name      :', os_name_strip)

if os_name == 'Windows':
    print('we have an OS match')
else:
    print('we do not have an OS match')

2 个答案:

答案 0 :(得分:2)

您已将实际函数 def repeate(c,rep): c=input("what str") rep=int(input("how many times?")) for i in range(rep): print(c*rep) return i repeat=repeate(0,0) print(repeat) 分配给您的 platform.system 变量。要执行函数并存储输出值,请确保包含括号:

os_name

答案 1 :(得分:1)

试试这个;

import platform
os_name = platform.system()
os_name_strip = os_name.strip()

print('OS Name      :', os_name_strip)

if os_name == 'Windows':
    print('we have an OS match')
else:
    print('we do not have an OS match')

OS Name      : Windows
we have an OS match