关于python的type函数问题
题目:Assumethattwovariables,varAandvarB,areassignedvalues,eithernumbersorstrings.Writea...
题目:Assume that two variables, varA and varB, are assigned values, either numbers or strings.
Write a piece of Python code that prints out one of the following messages:
"string involved" if either varA or varB are strings
"bigger" if varA is larger than varB
"equal" if varA is equal to varB
"smaller" if varA is smaller than varB
我这样写
if type(varA) or type(varB) ==type< 'str'>:print('string involved')
if varA > varB:print('bigger')
elif varA == varB:print('equal')
elif varA < varB:print('smaller')
总是第一句不对。它说:
File "submission.py", line 1
if type(varA) or type(varB) ==type< 'str'>:print('string involved')
^
SyntaxError: invalid syntax
我不明白…… 展开
Write a piece of Python code that prints out one of the following messages:
"string involved" if either varA or varB are strings
"bigger" if varA is larger than varB
"equal" if varA is equal to varB
"smaller" if varA is smaller than varB
我这样写
if type(varA) or type(varB) ==type< 'str'>:print('string involved')
if varA > varB:print('bigger')
elif varA == varB:print('equal')
elif varA < varB:print('smaller')
总是第一句不对。它说:
File "submission.py", line 1
if type(varA) or type(varB) ==type< 'str'>:print('string involved')
^
SyntaxError: invalid syntax
我不明白…… 展开
展开全部
import types
types.StringType
这里的types.StringType就是type("")
types.StringType
这里的types.StringType就是type("")
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-10-20
展开全部
if type(varA) == type("") or type(varB) == type(""):print('string involved')
更多追问追答
追问
唔,为什么用“type(“”)”呢?
这个“”是指里面是string?
追答
嗯,看下面的例子
>>> str
>>> type("")
>>> type("")==str
True
>>>
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询