0
x = int(input())
y = int(input())
print(x + y)

In running code online, using 2 input() like the one above automatically excludes the \n. The code when ran in pycharm does not run anything, instead it gets an error because it includes \n inside the input(). Is there a way to configure the settings to fix this? If not, is there any text editor that someone would recommend for a beginner who will solve simple coding problems or should I just stick to running code online?

SuperMage1
  • 131
  • 6
  • What do you mean when you say "pycharm [..] includes \n inside the input()"? If you run this code and enter text on the console, no `\n` will be included in the values of `x` and `y` – Grismar Oct 06 '21 at 01:04
  • invalid literal for int() with base 10: '5\n5' is the error message – SuperMage1 Oct 06 '21 at 01:06
  • Try [this answer](https://stackoverflow.com/a/1841607) to strip the EOL `\n` from the input before passing it as an argument to `int()`. – bad_coder Oct 06 '21 at 01:27

1 Answers1

0

Updated:

you need to remove non-digit with regex

import re
y = int(''.join(re.findall(r'\d+', input())))
x = int(''.join(re.findall(r'\d+', input())))
print(x + y)

or if you want to avoid regex you can do it by looping over your string, char by char, and chack witch one is a digit

y = int(''.join(ch for ch in input() if ch.isdigit()))
x = int(''.join(ch for ch in input() if ch.isdigit()))
print(x + y)
flaxon
  • 928
  • 4
  • 18
  • We are given machine problems in class and when I copy paste the sample input it include \n in the input is my problem – SuperMage1 Oct 06 '21 at 01:11
  • are you on linux, or mac? – flaxon Oct 06 '21 at 01:12
  • I run on windows 11, is that the answer you were looking for? The thing is I have been solving problems in hackerrank all this time and I would want to solve offline. in hackerrank when you have input() on 2 separate lines, and have custom input separated by a newline it automatically separates the input – SuperMage1 Oct 06 '21 at 01:16
  • oh i think i got your problem, your are pasting a value that has a `5\n` in it – flaxon Oct 06 '21 at 01:18
  • Yes, To make it work, I have to press 5, enter then 5 enter to separate, in hackerrank it kinda gets the input in one go. is this a limitation of pycharm itself or are there settings for it is my question – SuperMage1 Oct 06 '21 at 01:19
  • updated answer, let me know if it solve your problem – flaxon Oct 06 '21 at 01:23
  • It doesn't work sorry (when I input 5 then shift enter 5, the value inside the x is 55 and the program waits for a 2nd input before it successfully runs, i als dont want to try and add some imports because we are submitting machine problem solutions online like hackerrank, it looks like its a limitation of pycharm? Where do people usually solve problems in competitive programming? – SuperMage1 Oct 06 '21 at 01:33
  • why you press shift enter? – flaxon Oct 06 '21 at 01:34
  • when i pres shift enter it show me a popup `This view is read-only` – flaxon Oct 06 '21 at 01:35
  • to get to the new line, most problems we encounter is separated by new line, or either I copy-paste the sample input (separated by a new line) in hackerrank the problem does not automatically run and still waits for the 2nd input because it somehow concatenated the 1st input – SuperMage1 Oct 06 '21 at 01:35
  • so how about you have 1 `input()` and you split the string by `\n`?? – flaxon Oct 06 '21 at 01:37
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/237862/discussion-between-supermage1-and-flaxon). – SuperMage1 Oct 06 '21 at 01:37