This question already has answers here:
hash function in Python 3.3 returns different results between sessions
(3 answers)
Closed 4 months ago.
I am practising Python questions on HackerRank. Here's the question.
This is my solution:
if __name__ == '__main__':
n = int(input())
i = tuple(map(int,input().split()))
print(hash(i))
Desired Output is
3713081631934410656
However, the output I get is
-3550055125485641917
Why is that the case?
Someone tested my code, and it gave them the desired code. I tried copying and pasting my code again. Still didn't work.
Edit
It started working. I had to select Pypy3 (shoutout Bereal). Also, big ups to Chepner for pointing out Python3 Hash functionality.
From the discussion section: "THE TEST is for PYPY3, not Python3. Make sure to change language to Pypy3 !!!"
source: https://www.hackerrank.com/challenges/python-tuples/forum
In general #Inshaullah, it's best to check out the discussion forums on these sites if there's problems along these lines.
Related
This question already has answers here:
How can I print variable and string on same line in Python? [duplicate]
(18 answers)
Closed 2 years ago.
Ok so I'm working on a problem in my python class. I've gotten most of it figured out aside from print statements. I assigned the arguements correctly (I think) and am just trying to get the text to print correctly on the terminal side. What am I doing wrong here?
here is what I currently have
is the example that mine is supposed to look similar to
you could use f string print statements too like this:
print(f"distance in knots is: {distance_in_knots}")
This question already has answers here:
What's the difference between `raw_input()` and `input()` in Python 3? [duplicate]
(6 answers)
Closed 3 years ago.
I wrote s small script to iterate through a dictionary in python3 that works just fine. I was working in a different machine that only had python2.4 installed. I copied the script and ran and now the code is not entering the if-statement within the for loop. I am assuming this is just a version discrepancy.
I had tried to look online to see what some differences could be between versions. Closest I have come to is 'dict.iterkeys()'
tests = {'1':'test1','2':'test2'}
answer = input('which test? ')
for test in tests:
if test == answer:
print(tests[test])
The expected output is for the tests I want to be printed. However, in python version 2.4 it is not entering the if-statement at all. In python3 this script works just fine.
Any insight helps.
Thanks!
Python3 replaced the old input statement with the functionality of python 2’s raw_input. It used to evaluate the input now it’s passed as a string for safety.
Replace the line: (py3)
answer = input('which test? ')
With: (py2)
answer = raw_input('which test? ')
Or:
answer = str(input('which test? '))
Refer to PEP3111 for more details.
This question already has answers here:
Python Syntax Errors with Python Anywhere
(2 answers)
Closed 5 years ago.
I'm just starting to learn Python using a raspberry pi. One of the exercises on the pi website has the following code:
for i in range(2):
print("A")
print("B")
Which the tutorial says should give the output:
A
A
B
However, when I run this code in the Python 3.5.3 IDLE, I get a syntax error, with the second "print" highlighted. Any thoughts? Here is the website I'm referring to:
https://www.raspberrypi.org/documentation/usage/python/
I believe the the second print should be indented as well because python relies on white space instead of parenthesis or brackets, so the second print isnt being counted, however, im not super great at python
This question already has answers here:
Asking the user for input until they give a valid response
(22 answers)
Closed 8 years ago.
I have a program that gets a string from a user (raw_input) and sees if the length of it is greater than 8, for example. If it isn't it starts again, otherwise the program ends.
I'm using Python 2.7.8 and this is the code I'm using so far, but it doesn't work...
password = raw_input("Type in a password: ")
if password.len() > 8 :
print "Successful!"
else :
goto(1)
Can anyone solve this problem?
You need to learn the basics. You should start with a Python tutorial.
Here's the official Python tutorial from python.org; this section introduces the while statement, which you can use to make a loop.
https://docs.python.org/2/tutorial/introduction.html#first-steps-towards-programming
This question already has answers here:
Syntax error on print with Python 3 [duplicate]
(3 answers)
Closed 9 years ago.
I am a amature in Python,using the python33 version.The problem i am facing while i want to get output of a list of dictionary just like that.
dict = {'name':'Tanvir','Position':'Programmer'};
print dict['name'];
if i run the code then there showing a syntax error.same thing is happening for the list also.
Please help me to fix the problem.
Thanks in advance.
In Python 3, print is a function, so you need print(dict[name]). You also don't need the semicolons. You also need to read the Python tutorial to learn the basics first.