-4

Here is my current code. I would like it to be in one line I don't know how to though so I came here.

if int(input("enter num")) %2 == 0:
print("yay") 
  • 1
    Are you talking about `print( '123' * len(i))`? Isn't that the same thing? – Tim Roberts Jul 16 '22 at 21:16
  • Do you mean every _character_ or every _letter_? So, if the input is "U2", should it produce "123123" or "1232"? – Ignatius Reilly Jul 16 '22 at 21:18
  • Your current code has a logic problem. Let's say the input is `'abc'`. You first replace the `a`, so you have `'123bc'`. Next, you look at character 1, but that's now a 2. So, you replace the 2 and get `'11233bc'`. Next, you look at character 2, which is a 2. This is not what you want. – Tim Roberts Jul 16 '22 at 21:19
  • 1
    Can you clarify? It would be helpful if you could provide an example what the user types in in response to the "enter here" prompt, and what it is you'd like to see displayed as a result of that. – codingatty Jul 16 '22 at 21:23
  • @Kyle Oakes What are you trying to do??, you didn't specify – Capt.Pyrite Jul 16 '22 at 21:24
  • @FahimFerdous, the question is completely clear. – Imago Jul 16 '22 at 21:25
  • 2
    @Kyle Ooakes , what does the expected output supposed to look like? – Capt.Pyrite Jul 16 '22 at 21:25
  • @IgnatiusReilly 2 is not a letter, but a number. – Imago Jul 16 '22 at 21:25
  • @Imago , If it was this clear why didn't you answer the question yet? – Capt.Pyrite Jul 16 '22 at 21:27
  • @Kyle Ooakes you want to compress your code into 1 line? – Capt.Pyrite Jul 16 '22 at 21:28
  • @FahimFerdous I recommend reading the SO guidelines when someone is supposed to answer a question and when not. I don't suffice all requirements – Imago Jul 16 '22 at 21:29
  • 2
    @Imago, that's exactly why I'm asking: the title says "every letter", but the strategy in the OP's code suggest she wants to replace every character. Not everybody communicates in perfect English (including myself) and sometimes it's necessary to help the OP to clearly convey her idea. – Ignatius Reilly Jul 16 '22 at 21:29
  • @Imago the question doesn't suffice all requirements either- – Capt.Pyrite Jul 16 '22 at 21:35
  • @FahimFerdous, definitely, but at least he tried which is way above average – Imago Jul 16 '22 at 21:40
  • @Imago and im trying to help, which is under my average – Capt.Pyrite Jul 16 '22 at 21:44
  • Downvoting as asker is not responding to comments of people who want to help. – trincot Jul 16 '22 at 22:06

2 Answers2

2

Since your question wasn't "completely clear". Such as what the output is supposed to be, and if you wanted to compress your code into 1 line or not...

But try this:

  • #1st code~ returns output like: ['123ello', 'h123llo', 'he123123o', 'he123123o', 'hell123']

  • #2nd code ~ returns output like: 123123123123123 (@Tim Roberts's comment)

1 line versions

#1

i = input(">");print([i.replace(i[d],"123") for d in range(len(i))])

#2

x = input(">");print('123' * len(i))

Multiline versions

#1

i = input(">")
x = []
for d in range(len(i)):
  x.append(i.replace(i[d],"123"))
print(x)

#2

x = input(">")
print('123' * len(i))
Capt.Pyrite
  • 851
  • 7
  • 24
  • 2
    As a side note: In Python, strings are [immutable](https://stackoverflow.com/questions/8680080/why-are-python-strings-immutable-best-practices-for-using-them). Therefore, when you "replace" a character, under the hood Python is building a new string and reassigning it to the variable. That's why the solution `'123' * len(i)` is better than any complicated algorithm you could think of. – Ignatius Reilly Jul 16 '22 at 22:06
  • @Ignatius Reilly, All credits to @ Tim Roberts for his comment – Capt.Pyrite Jul 16 '22 at 22:18
0

The following code works with regex. Does it give the desired result?

import re
i = 'i2t'
print(re.sub('[^\W\d_]', '123', i))

The output from above will be: 1232123

Imago
  • 521
  • 6
  • 29