1

I am trying to write a piece of code that takes a string as input and replaces * for a range of numbers, sort of like:

I am * years old

0-10

I am 0 years old

I am 1 years old

...

This is an attempt to make my life easier on R while I also learn some Python. Here is my code:

yourtext = input(" ")
number = input(" ")
number = int(number)

while number > 0:
    i = str(number)
    print(i)
    yourtext = yourtext.replace("*", i)
    print(yourtext)
    print(number)
    number = number - 1

And right now, this returns:

*
5 (my inputs)

5
5
5
4
5
4
3
5
3
2
5
2
1
5
1

So it seems like the i inside the replace function is fixed at its value on the first iteration of the loop. Why is this and how can I change it? Thanks a lot for your help.

2
  • 1
    Can you make sure your indents are placed correctly for each line? Python is indent sensitive Commented Oct 9, 2018 at 21:30
  • 1
    Please fix the indentation in your question Commented Oct 9, 2018 at 21:30

5 Answers 5

1

After you replace * with a number, the next iterations of the loop will do nothing. There are no more * in yourtext.

yourtext = yourtext.replace("*", i)
Sign up to request clarification or add additional context in comments.

Comments

0

I'd ditch the while loop in favor of for, since you could get caught in an infinite loop that way. Also, for will take a range of values so you don't have to manually increment your value:

yourtext = input("What would you like? ")
number = int(input("What is the number? ")

if '*' not in yourtext:
    raise ValueError("could not find wildcard")

for i in range(number):
    print(yourtext.replace("*", i)

Comments

0

You're really close! Your only problem here is that the line:

yourtext = yourtext.replace("*", i)

is updating the yourtext variable. So after your first iteration through the loop, you've changed the "*" in yourtext to be the number. So instead of yourtext being the string "I am * years old" (in your original example) it gets changed to "I am 5 years old". Then, when you go through the loop again, there is no "*" to replace (because the string "I am 5 years old" has no *), so the text stays as "I am 5 years old" instead of changing to "I am 4 years old".

You can fix that by making a new temporary variable instead, for example replacing the problem line with:

updated_text = yourtext.replace("*", i)

Comments

0

Not sure what the purpose of yourtext is, but after you replace the "*" the first time, yourtext will not update since it doesn't contain a * anymore. For example, if you input "test" instead of *, yourtext will never update because there is no * in the first place

4
test
4
3
test
3
2
test
2
1
test
1

Comments

0

You could do this in a loop with string.replace()

s = 'I am * years old'
number = 10

for i in range(number +1):
    print('{}'.format(s.replace('*', str(i))))
I am 0 years old
I am 1 years old
I am 2 years old
I am 3 years old
I am 4 years old
I am 5 years old
I am 6 years old
I am 7 years old
I am 8 years old
I am 9 years old
I am 10 years old

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.