简体   繁体   English

我如何让程序找到一个可以正常工作的字符串?

[英]how do i make the program find a string to work properly?

i am looking to input two strings s1 and s2 and see if string s2 is in string s1 and if so where is it in string s1.我正在寻找输入两个字符串 s1 和 s2 并查看字符串 s2 是否在字符串 s1 中,如果是,它在字符串 s1 中的什么位置。 But there is an error that causes the program to run nothing.但是有一个错误导致程序什么也不运行。 can you help me find the error and help me fix it?你能帮我找出错误并帮我解决吗? The language my program is using is Python.我的程序使用的语言是 Python。 Thank you!谢谢!

import os
os.system('cls')

print('nhap chuoi 1: ',end='')
s1=str(input())
print('nhap chuoi 2: ',end='')
s2=str(input())
tontai=s2 in s1
if tontai==True:
    i=0
    while i<=len(s1)-1:
        v=0
        if s2[v]==s1[i]:
            j=i+1
            kt=False
            for j in range(i+1,i+(len(s2))):
                v+=1
                if s1[j]==s2[v]:kt=True
                else:
                    kt=False
                    break;
            if kt==True: 
                print(i,end=' ')
                i=i+len(s2)
        else:i+=1
else:print('chuoi 2 khong nam trong chuoi 1')
stop=input()

Python has a function to do what you want: Python 有一个 function 做你想做的事:

import os
os.system('cls')

s1 = input('Enter string 1: ')
s2 = input('Enter string 2: ')
pos = s1.find(s2)
if pos == -1:
    print('s2 is not in s1')
else:
    print('s2 is in s1 at the position', pos)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM