Hey everyone
Hello everyone. How are you? I'm back, so you're probably annoyed. So, anyways, I was writing this code, and I'm not sure how to do something. Help me out, please?
So in this code I have to get a series of numbers and print the first pair of consecutive numbers that share the same sign (+/-). And if there are no consecutive numbers that share the same sign, I have to print 0. So, I've managed to do the first part with a for loop, but I'm clueless on how to do the second part. Can you guys give me some advice please? Thanks!
Here's the code:
a = [int(s) for s in input().split()]
w = []
e = 1
for i in range(len(a)):
if a[e] > 0 and a[e-1] > 0 or a[e] < 0 and a[e-1] < 0:
print(a[e-1] , a[e])
break
else:
e += 1
continue
What is the variable e
for? couldn't you just use i
? As for the solution, just set a variable, such as done
to False
at the beginning of your program and set it to true if you find a pair. If the variable is still false when you are done with the loop, print 0
.
I am repl.it helper. If this is a good answer, please upvote!
Thanks a lot!
Please upvote if this helps :)