Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 10 months ago.
Improve this question
pist = ['P58', 'P60', 'P0']
def print_it:
For i in pist:
if i == 'P0':
print('yes')
elif i == 'P58':
print('yup')
else:
print('No')
So this is the issue:
I want the function to print 'yes' as long as P0 is in the list(irrespective of whether P58 is in it) but it keeps printing 'yup' because P58 is the first item on the list. Can there be another way?
Note: the list cannot be changed.
def print():
if 'P0' in pist:
return 'yes'
elif 'P58' in pist:
return 'yup'
else:
return 'No'
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Need some help here
num = [(1,4,5,30,33,41,52),(2,10,11,29,30,36,47),(3,15,25,37,38,58,59)]
if the last 6 digits are located to return the first digit.
example if finds 10,11,29,30,36,47 return 2
You can use next similair to user's approach:
num = [(1,4,5,30,33,41,52),(2,10,11,29,30,36,47),(3,15,25,37,38,58,59)]
to_find = [10,11,29,30,36,47]
print(next(n for n, *nums in num if nums == to_find))
2
You can use next with a conditional generator expression:
num = [(1,4,5,30,33,41,52),(2,10,11,29,30,36,47),(3,15,25,37,38,58,59)]
search = 11
next(first for first, *rest in num if search in rest)
# 2
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
def CodelandUsernameValidation(s):
if len(s)>4 and len(s)<25 and s[0].isalpha() and [i for i in s if i.isalnum() or i=="_"]!=[] and s[-1]!="_":
return True
else:
return False
# keep this function call here
print(CodelandUsernameValidation(input()))
It's a list comprehension if you expand, it'll look like this ->
result = []
for i in s: # will fetch element one by one from iterable
if i.isalnum() or i=="_": # checking condition
result.append(i) # if true, then append it to the list
This above code can be rewritten as -
result = [i for i in s if i.isalnum() or i=="_"]
That produces a list of those characters in s that are either alphanumeric or underlines. The code is actually incorrect, because it will pass if ANY of the characters are alphanumeric or underline, whereas the intent surely was that ALL of the characters must be alphanumeric or underline. Here's a better way to write it:
def CodelandUsernameValidation(s):
return 4 < len(s) < 25 and s[0].isalpha() and all(i.isalnum() or i=='_' for i in s) and s[-1] != '_'
print(CodelandUsernameValidation(input()))
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Im new to python.i have absolutly no idea how to solve this:
Extends the list with a While loop or For loop, so that the next entry is the sum of the last entry and the previous entry:
myListe = [1,2,3]
Deducing from your given values, you are starting out from
myListe = [1, 2, 3]
# ^ ^
# index -3 -1
and are supposed to add the last element and the one two before that.
This, you can achieve via:
while True:
next_value = myListe[-3] + myListe[-1]
if next_value > 1000:
break
myListe.append(next_value)
You can verify that the last value in the list is then in deed 872.
myListe[-3:]
# [406, 595, 872]
Using some knowledge about built-in utils, slices and named assignments (Python >= 3.8), you can simplify the while loop:
while (next_value := sum(myListe[-3::2])) <= 1000:
myListe.append(next_value)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
this code is meant to 'explode' a given string s
def string_splosions(s):
"""erp9yoiruyfoduifgyoweruyfgouiweryg"""
new = ''
for i in s:
new += s[0:int(s.index(i))+1]
return new
For some reason this code can return the correct 'explosion' for most words however words which have a repeated letter they do not print correctly.
examples.
Correct outputs is would be:
Code --> CCoCodCode
abc --> aababc
pie --> ppipie
incorrect outputs when s is
Hello --> HHeHelHelHello (should be HHeHelHellHello)
(Note: in the incorrect output there should be 1 more l in the second to last repeat.)
You should transcribe the code instead of posting a picture:
def string_splosion(s):
new = ''
for i in s:
new += s[0:int(s.index(i))+1]
return new
The problem is that index(i) returns the index of the first instance of that character, which is 2 for both l's in "Hello". The fix is to just use the index directly, which is also simpler:
def string_splosion(s):
new = ''
for i in range(len(s)):
new += s[:i+1]
return new
Or even:
def string_splosion(s):
return ''.join(s[:i+1] for i in range(len(s)))