Python不接受某些数字输入
有了这段代码,我所要做的就是在奇数之间插入破折号,在偶数之间插入星号。它不能在每次输入时都正常工作。它与46879一起工作,但与468799一起不返回任何值,或与4546793一起不在4和6之间插入*。它为什么这样做?谢谢Python不接受某些数字输入,python,Python,有了这段代码,我所要做的就是在奇数之间插入破折号,在偶数之间插入星号。它不能在每次输入时都正常工作。它与46879一起工作,但与468799一起不返回任何值,或与4546793一起不在4和6之间插入*。它为什么这样做?谢谢 def DashInsertII(num): num_str = str(num) flag_even=False flag_odd=False new_str = '' for i in num_str: n = int(i) if n % 2 =
def DashInsertII(num):
num_str = str(num)
flag_even=False
flag_odd=False
new_str = ''
for i in num_str:
n = int(i)
if n % 2 == 0:
flag_even = True
else:
flag_even = False
if n % 2 != 0:
flag_odd = True
else:
flag_odd = False
new_str = new_str + i
ind = num_str.index(i)
if ind < len(num_str) - 1:
m = int(num_str[ind+1])
if flag_even:
if m % 2 == 0:
new_str = new_str + '*'
else:
if m % 2 != 0:
new_str = new_str + '-'
else:
return new_str
print DashInsertII(raw_input())
def DashInsertII(num):
num_str=str(num)
flag_偶数=False
flag_odd=False
新的_str=''
对于num_str中的i:
n=int(i)
如果n%2==0:
flag_偶数=True
其他:
flag_偶数=False
如果n%2!=0:
flag_奇数=真
其他:
flag_odd=False
new_str=new_str+i
ind=数量索引(i)
如果ind
您的函数定义是我见过的最过度构建的函数之一;下面的内容应该是你想做的,而不考虑复杂性
def DashInsertII(num):
num_str = str(num)
new_str = ''
for i in num_str:
n = int(i)
if n % 2 == 0:
new_str += i + '*'
else:
new_str += i + '-'
return new_str
print DashInsertII(raw_input())
编辑:我刚刚重读了这个问题,发现我误解了你的意思,即在两个奇数之间插入一个-
,在两个偶数之间插入一个*
。为此,我能想出的最佳解决方案是使用正则表达式
第二次编辑:根据的要求,我在这篇文章中加入了对正则表达式的解释
import re
def DashInsertII(num):
num_str = str(num)
# r'([02468])([02468])' performs capturing matches on two even numbers
# that are next to each other
# r'\1*\2' is a string consisting of the first match ([02468]) followed
# by an asterisk ('*') and the second match ([02468])
# example input: 48 [A representation of what happens inside re.sub()]
# r'([02468])([02468])' <- 48 = r'( \1 : 4 )( \2 : 8 )'
# r'\1*\2' <- {\1 : 4, \2 : 8} = r'4*8'
num_str = re.sub(r'([02468])([02468])',r'\1*\2',num_str)
# This statement is much like the previous, but it matches on odd pairs
# of numbers
num_str = re.sub(r'([13579])([13579])',r'\1-\2',num_str)
return num_str
print DashInsertII(raw_input())
重新导入
def DashInsertII(数量):
num_str=str(num)
#r'([02468])([02468])对两个偶数执行捕获匹配
#相邻的
#r'\1*\2'是一个字符串,由后面的第一个匹配([02468])组成
#通过星号('*')和第二个匹配([02468])
#输入示例:48[表示re.sub()内部发生的情况]
#r'([02468])([02468])”如果我了解您的问题-对于11223344
您需要1-12*23-34*4
def DashInsertII(num):
prev_even = ( int(num[0])%2 == 0 )
result = num[0]
for i in num[1:]:
curr_even = (int(i)%2 == 0)
if prev_even and curr_even:
result += '*'
elif not prev_even and not curr_even:
result += '-'
result += i
prev_even = curr_even
return result
print DashInsertII(raw_input())
RevanProdigalKnight的答案几乎正确,但当3个或更多的偶数/奇数加在一起时,答案就失败了
使用正则表达式执行此操作的正确方法是使用正向前瞻(使用?=)
这将产生:
234*4*67-7-76*6*678*8*8 ( what is desired)
234*467-776*6678*88 ( not what we wanted)
使用4个空格或制表符缩进代码。这几乎不可读。另外,您没有在函数末尾添加返回。num\u str.index(i)
将始终返回第一次出现的值。如果索引返回第一个匹配项而不是下一个匹配项,则不能期望ind+1
指向下一个数字。此代码可以简化和清理。@阿尔维茨他可以通过在循环签名中使用枚举(num_str)来获得索引。@RafaelBarros-尽管您的建议可以解决他的问题,这并不能让他知道自己哪里出了错。我认为最快的学习方法是了解他们哪里出了问题。您应该将您的建议作为备选答案之一发布,我也会投票表决。+1您可以在正则表达式中添加一些解释,以便于OP和其他可能无意中进入此线程的人。@Alv这是一个很好的建议,我已经编辑了此帖子,将其包括在内。将数字转换为字符串后,效果非常好。非常整洁,谢谢raw_input()
始终提供字符串,因此我从相关版本中删除了str()
。
234*4*67-7-76*6*678*8*8 ( what is desired)
234*467-776*6678*88 ( not what we wanted)