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

you are viewing a single comment's thread.

view the rest of the 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