Search code examples
python-3.xpycharmprogram-entry-point

Can read inputs but unable to execute script in Pycharm


I recently started using Pycharm and I was trying to execute the starter code provided by one of my online classes from coursera. The code is to find gcd of two numbers:

import sys

def gcd_naive(a, b):
current_gcd = 1
for d in range(2, min(a, b) + 1):
    if a % d == 0 and b % d == 0:
        if d > current_gcd:
            current_gcd = d

return current_gcd

if __name__ == "__main__":
input = sys.stdin.read()
a, b = map(int, input.split())
print(gcd_naive(a, b))

I am only able to input the two numbers and the script doesn't execute after that at all and doesn't throw any errors either. I have attached the screenshot of my issue.enter image description here

I'd really appreciate a push in the right direction


Solution

  • If you're using sys.stdin.read() to get user input, you have to end your input with Ctrl+Z or Ctrl+C (in Windows) or Ctrl+D in Linux. However for some reason it doesn't work in PyCharm console/debug.

    You can use the standard way of reading user input (using input() function), just change your main function to this:

    if __name__ == "__main__":
        a, b = map(int, input().split())
        print(gcd_naive(a, b))