我坚持实现一些 if,elif 和 else 语句



我正试图完成一项任务,但到目前为止我所做的一切都失败了。

def valid_format(s):

pass

z = introcs.count_str(s,',')
x = introcs.split(s,',')
y = introcs.find_str(s,',')
if len(s) >= 2:
if s[0] == '0':
return False

if len(s) <= 3:
if introcs.isdigit(s):
x = int(s)
if x >= -1 and x <= 999:
if z == 0:
return True
else:
return False
else:
return False

if len(s) >= 4 and len(s)<= 7:
if introcs.isdigit(s):
x = int(s)
if x >= 999 and x <= 1000000:
return True
else:
return False
else:
return False

我似乎不太擅长if的陈述,因为每次我尝试新的东西,它都会给我同样的结果。在我尝试使用带逗号的数字(例如1230(之前,代码一直有效(?(。任何关于我做错了什么或应该改变什么的提示和解释都将不胜感激!

编辑:很抱歉没有提供足够的详细信息。如果s是有效的数字字符串并且必须少于7个字符,则函数需要返回True。我在逗号方面有困难。我得到了相反的答案(正确或错误(

例如:当它应该是假的时,我得到了真

valid_format('2389')
True

但当它应该是True 时为False

valid_format('2,389')
False

我需要这种类型的结果:

valid_format('2,389')
True
valid_format('32,45')
False
count=0
for i in range(len(s)-1,-1,-1):
count+=1
if count%4==0:  #make sure every 4th char is a ','
if s[i]==',':
pass
else:
return False
else:
if ord(s[i])-ord('0')<0 or ord(s[i])-ord('0')>9: #check if char is valid 0-9 using ascii arithmetic 
return False
else:
pass
if i==0:
if ord(s[i])-ord('0')>9 or ord(s[i])-ord('0')<1: #check first digit is valid 1-9
return False
return True

最新更新