3

I'm using this code to try to replace a character:

from another_test import test_once_more
test_once_more()
question = input("1 letter ")
for letter in question:
    if letter == "a":
        test_once_more.replace("1","a")
        print (test_once_more)

This is the code I am using. All I want it to do is replace the 1 in this code.

def test_once_more():
    print ("123456789")

and replace it with an "A"

0

2 Answers 2

4

You can't.

The function is printing something and returns None. There's no way to change that after the fact.

What you should do is have the function return a value and work on that:

def test_once_more():
    return "123456789"

and then

from another_test import test_once_more
result = test_once_more()
question = input("1 letter ")
for letter in question:
    if letter == "a":
        result = result.replace("1","a")
        print (result)

although I'm puzzled why you're using a for loop to iterate over a string that will be a single character (at least if your user follows your request)...

Sign up to request clarification or add additional context in comments.

1 Comment

thank you for the help I am not that good at python still learning in school
0

You have a very simple error here; test_once_more is the name of your function, while question is the name of your string. Try question.replace (and fix the other similar mistake).

P.S. strings cannot be mutated in Python, so you need to assign the result of calling replace to another variable in order to see the effect of calling replace().

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.