为变量Python指定最大/最小字符数(字符串)
代码中有一些法语,但不用担心,我想在这里做的唯一一件事就是添加一些东西,防止我在ch1中输入4以下的字符串,在ch2中输入至少1个字符。这段代码适用于我正在做的事情为变量Python指定最大/最小字符数(字符串),python,string,Python,String,代码中有一些法语,但不用担心,我想在这里做的唯一一件事就是添加一些东西,防止我在ch1中输入4以下的字符串,在ch2中输入至少1个字符。这段代码适用于我正在做的事情 ch1 = input("Entre the first chain:") ch2 = input('Enter the second chain:') resultat = 0 sub_len = len(ch2) for i in range(len(ch1)): if ch1[i:i+sub_len] == ch2
ch1 = input("Entre the first chain:")
ch2 = input('Enter the second chain:')
resultat = 0
sub_len = len(ch2)
for i in range(len(ch1)):
if ch1[i:i+sub_len] == ch2:
resultat += 1
print('Chaîne 1 saisie: {}'.format(ch1))
print('Chaîne 2 saisie: {}'.format(ch2))
print('Réponse: La chaîne 2 se retrouve {} fois dans la châine 1.'.format(resultat))
您必须测试自己输入字符串的长度,如果输入无效,则在循环中重复输入 其中一种方法是:
while True:
ch1 = input("Entre the first chain: (minimum 4 chars)")
if len(ch1) >= 4:
break
while True:
ch2 = input('Enter the second chain (minimum 1 char):')
if len(ch2) >= 1:
break