Simple if and input doesn't work [duplicate] - python

This question already has answers here:
String comparison in Python: is vs. == [duplicate]
(4 answers)
Closed 6 years ago.
I wrote this very simple code, it doesn't work and I can't understand why. I answer 'good' but if part doesn't work!
def howdy ():
p = input("how's it going?")
if p is "good":
print("Cheers")
howdy()

You have to use:
If p == 'good':
The == compares the value of the two. The Is keyword checks for identity by comparing the memory address.
Hope this explains it enough.
Hannes

have you tried
if p == "good":
the double equal means the same as.
Also, if you are looking for a helpful free beginner course, try codecademy. They helped me loads learn python

Related

Hash function in Python is not giving the answer I want [duplicate]

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.

Is putting an "else" in this case relevant or not, and why? [duplicate]

This question already has answers here:
It is more efficient to use if-return-return or if-else-return?
(9 answers)
Closed 6 months ago.
I've always had the habit to put else statements but I'm wondering if there's any reason not to, if it works.
Here's an example:
def multiply_by_two(number, display_original_number=False):
result = number*2
if display_original_number is True:
return f'Multiplying {number}: {result}'
return f'{result}'
print(multiply_by_two(5, True))
print(multiply_by_two(5))
Output:
Multiplying 5: 10
10
If I put else: return f'{result}', it's bascially the same thing, right? Is there any reason I would want to put an else statement in such cases?
Within this use case there is no difference functionally. Personal preference would dictate this decision if you find it easier to read back your code using else: .

New to Python, trying to do print statements. College textbook is super short not being helpful [duplicate]

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}")

C command similar to "if x in list" of Python [duplicate]

This question already has answers here:
Check if a value exists in an array in Cython
(2 answers)
Closed 6 years ago.
In Python we could use if x in list: so I was wondering if there's a similar command in C, so that we don't have to go through the whole thing using a for.
How can you know whether a value is contained in an array without cycling through it? This is exactly what Python does under the hood. No, there's no magical way to instantly know this.

Repeat a set of code, until something is achieved - Python [duplicate]

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

Categories

Resources