Python Forum
HackerRank Problem: Code works on VS Code but not on the HackerRank site - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: HackerRank Problem: Code works on VS Code but not on the HackerRank site (/thread-32724.html)



HackerRank Problem: Code works on VS Code but not on the HackerRank site - Pnerd - Feb-28-2021

Hi, I'm trying to solve this HackerRank problem: https://www.hackerrank.com/challenges/py-set-add/problem

This is my code:

countries = set()
n = int(input())
for i in range(n):
    countries.add(input())
print(len(countries))
It works when I run it on VS Code, but gives the following error when I try to run the code on the HackerRank site:

Error:
Traceback (most recent call last): File "Solution.py", line 5, in <module> countries.add(input()) File "<string>", line 1, in <module> NameError: name 'UK' is not defined
What am I doing wrong here?
.


RE: HackerRank Problem: Code works on VS Code but not on the HackerRank site - ndc85430 - Feb-28-2021

Looks like they're using Python 2.x, as input used to do the same as eval, basically. Is there the option of using 3.x? If not, you'll need to use raw_input instead on Python 2.


RE: HackerRank Problem: Code works on VS Code but not on the HackerRank site - Pnerd - Feb-28-2021

[How can I delete this post?]


RE: HackerRank Problem: Code works on VS Code but not on the HackerRank site - Pnerd - Feb-28-2021

(Feb-28-2021, 06:22 PM)ndc85430 Wrote: Looks like they're using Python 2.x, as input used to do the same as eval, basically. Is there the option of using 3.x? If not, you'll need to use raw_input instead on Python 2.
Thanks. That was it. I chose Python 3 and tried again. And it worked. Thank you once again.