Mani
Mani

Reputation: 11

Python code is not working in Pycharm, but it executes well in Jupiter notebook

I have the following code:

from random import randint
pyPicInteger = randint(1,100);

print("Hello Genius, Lets introduce this game to you!\n\nThe game will get the lucky number, you just need to GUESS the luck number.\n\nThe winner will be who guess the number in the less attempts!!!")

print(pyPicInteger)

attempts=[0]

while True:
    playerInput = int(input("Guess the number: "))
    attempts.append(playerInput)
    if playerInput < 1 or playerInput > 100:
        print("Your guess is out of bound...Enter the value between 1 and 100!")
        continue
    if playerInput == pyPicInteger:
        print("YOU ARE THE WINNER NOW!!! YOU HAVE ACHEIVED IN {} ATTEMPTS".format(len(attempts)))
        break

    if attempts[-2]:
        if abs(pyPicInteger - playerInput) < abs(pyPicInteger-attempts[-2]):
            print("WARMER!")
        else:
            print("COLDER!")
    else:
        if abs(pyPicInteger-playerInput) <= 10:
            print("WARM!")
        else:
            print("COLD!")    

When I run it using PyCharm, I get the following error:

C:\Users\*********\PycharmProjects\Operaters\venv\Scripts\python.exe "C:\Program Files\JetBrains\PyCharm Community Edition 2018.1.4\helpers\pydev\pydevd.py" --multiproc --qt-support=auto --client 127.0.0.1 --port 52594 --file C:/Users/*********/PycharmProjects/Operaters/venv/GameChallenge1.py
pydev debugger: process 13700 is connecting

Connected to pydev debugger (build 181.5087.37)
Hello Genius, Lets introduce this game to you!

The game will get the lucky number, you just need to GUESS the luck number.

The winner will be who guess the number in the less attempts!!!
55
Guess the number:34
COLD!

 Guess the number: Traceback (most recent call last):
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2018.1.4\helpers\pydev\pydevd.py", line 1664, in <module>
    main()
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2018.1.4\helpers\pydev\pydevd.py", line 1658, in main
    globals = debugger.run(setup['file'], None, None, is_module)
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2018.1.4\helpers\pydev\pydevd.py", line 1068, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2018.1.4\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "C:/Users/*********/PycharmProjects/Operaters/venv/GameChallenge1.py", line 12, in <module>
    playerInput = int(input("Guess the number: "))
ValueError: invalid literal for int() with base 10: ''

Process finished with exit code 1

Upvotes: 0

Views: 844

Answers (2)

Martin Giroux
Martin Giroux

Reputation: 1

You're entering an empty string, as the message says. Validate your input before you convert it. Something like:

answer = input("Guess the number: ")
if not answer.isdigit(): continue
playerInput = int(answer)

That ought to do the trick.

Upvotes: 0

lmiguelvargasf
lmiguelvargasf

Reputation: 70003

As @101 has commented, it seems you are running different versions of Python in Pycharm and in Jupyter. In PyCharm you can change by:

  1. In the Settings/Preferences dialog click Project Interpreter.

enter image description here

  1. Click gear icon and choose Add .... py add interpreter The Add Python Interpreter dialog box opens.

enter image description here

  1. Choose the base interpreter from the drop-down list, or click browseButton and find the base interpreter in the your file system. You should give the path to the interpreter that uses jupyter.

Another reason you might have this error is because you are just entering an empty string when the program is expecting for a number, so please avoid doing that since int('') will give you the same error you are reporting:

ValueError: invalid literal for int() with base 10: ''

Upvotes: 2

Related Questions