python - 为变量 Python 提供最大/最小字符数(字符串)
问题描述
代码中有一些法语,但不用担心,我在这里唯一想做的就是添加一些内容,以防止我在 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:
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
推荐阅读
- oracle - Oracle 侦听器状态显示在 DBCA 中
- python - 如何在 djangorestframework 中为我在使用世界天气在线 api 的数据后写入的 csv 文件创建下载 api 端点
- java - String[] 选项只显示一次
- python - 使用 BeautifulSoup 获得第二个 Span
- spring-boot - 在 Java 中路由到主页失败
- tensorflow - 如何将通道第一个 Keras 模型转换为通道最后一个模型?
- yosys - 使用 yices 进行形式验证——破管
- graphql - Shopify 使用 GraphQL 查找 SKU
- javascript - 使用 html 和 javascript 填充文本框
- python - ax.text 对 (x,y) 使用组合的 transData 和 transAxes