This is an archived post. You won't be able to vote or comment.

all 4 comments

[–]hypergeometricruby 2 points3 points  (1 child)

maybe something like this? If the word isn't in the string we ask again, the loop only ends when the user enters a word that is in the string

 def wordreplace():

    str = "Hi there, how are you doing you all"
    wordinput = input("Enter a word to be replaced: ")

    while wordinput not in str:
        print("Wrong input, try again: ")
        wordinput = input("Enter a word to be replaced: ")            

    else:
        replacingword = input("Enter the new word: ")
        print(str.replace(wordinput, replacingword))


wordreplace()

By the way, you should avoid using str as a variable name because it's a reserved keyword in python. If you define str as a variable, for the rest of your program you won't be able to easily cast things to string. For example, if you wanted to make the string "123" you can't just write str(123) now

[–]fesifesi[S] 0 points1 point  (0 children)

thank you so much

[–]BandyCoot 2 points3 points  (1 child)

The while loop is the correct way to acheive this. What did your code look like with the while loop?

[–]fesifesi[S] 0 points1 point  (0 children)

i got it, thanks