Is there a fundamental error in this code? [closed] - python

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 2 days ago.
Improve this question
I tried solving a for loop practice question on Sololearn and it says 'try again'... but my answer turned green, which means successfully solved, and resulted in the correct/expected output.
I'm a beginner, and I'm sure there are other ways to write this out... but is there some fundamental error in this code? Or does this work every time?
x = 'python is great, its awesome!'
v = 0
for c in x:
if (c == 'a'):
v = v + 1
elif (c == 'e'):
v = v + 1
elif (c == 'i'):
v = v + 1
elif (c == 'o'):
v = v + 1
elif (c == 'u'):
v = v + 1
else:
pass
print(v)
Once again I'm aware there's a more efficient way to write this out but I'm just wanting to know if this longhand version is correct or not since its giving the correct output but saying try again.

Related

How do I optimize nested if statements? I cannot use && here [closed]

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
Is there a better way in which this can be written? I'm new to python coding and I want to improve below lines of code. I have included a few lines of my code here but the original code is much longer than this(multiple if statements). Any guidance/feedback would be helpful.
upperchar = #generate a char
pwdlist.append(upperchar)
if len(pwdlist) < length:
upperchar2 = #generate a char
pwdlist.append(upperchar2)
if len(pwdlist) < length:
lowerchar = #generate a char
pwdlist.append(lowerchar)
if len(pwdlist) < length:
lowerchar2 = #generate a char
pwdlist.append(lowerchar2)
if len(pwdlist) < length:
char3 = #generate a char
pwdlist.append(char3)
Posting my first question here so if you need anything more please ask me.
Assuming you are always generating a character the same way, then this would work
pwdlist = []
while len(pwdlist) < length:
pwdlist.append(generate_char())
Or
pwdlist = [generate_char() for _ in range(length)]
Then you can add logic into this generate function to return something based on random values, for example
def generate_char():
from random import randrange
x = randrange(4)
if x == 0:
return 'value a-z'
elif x == 1:
return 'value A-Z'
elif x == 2:
return 'value 0-9'
elif x == 3:
return 'punctuation'

I'm returning a defaultdict(list), but randomly choosing between the two, why does it return nothing sometimes? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
output = ""
numberList = [0, 1]
print(random.choice(numberList))
if(random.choice(numberList) == 0):
if len(slots) > 0:
output = templates[state][0].replace("<num_classes>", str(slots[0][1]))
else:
output = templates[state][0]
elif(random.choice(numberList) == 1):
if len(slots) > 0:
output = templates2[state][0].replace("<num_classes>", str(slots[0][1]))
else:
output = templates2[state][0]
return output
expected answer
sometimes get this
Sometimes it returns nothing or no dictionary... Why?
With
elif(random.choice(numberList) == 1)
you will again choose a brand new random number. And if that isn't 1 then there's no else that will set output.
Instead of elif you should have a plain else.

Deleting max value in set without max() [closed]

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 2 years ago.
Improve this question
I have a working code
task = set()
x = 1
while x != 0:
x = int(input('input your number '))
task.add(x)
print('Just 0 could stop it!')
task.remove(max(task))
print(max(task))
And need to get the same result without using max(). What could be an alternative?
Something like this, unless you have really large sets, I don`t see the advantage
task = set()
x = 1
m = 0
while x != 0:
x = int(input('input your number '))
task.add(x)
if x > m:
m = x
print('Just 0 could stop it!')
task.remove(m)
print(max(task))
Notice this will only work for positive numbers, if you want to the complete int range you should init m like this m = -sys.maxsize - 1
You could use min with a key arg that inverts the element:
>>> task = {1, 2, 3, 4, 5}
>>> max(task)
5
>>> min(task, key=lambda x: -x)
5
Or you could sort it and take the last element...
>>> sorted(task)[-1]
5

Compare values stored in variables, then print the variable name [closed]

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 5 years ago.
Improve this question
I have tried to use the max code but it printed out the largest number not the variable name.
a=3
b=4
c=7
d=6
e=8
max(a,b,c,d,e)
this is the code I've tried but it printed 8 instead of e.
forgot to add that my values for a b c etc came from a loop so im not suppose to know their values myself...
You need to use a dictionary or a named tuple to do something like that.
>>> my_list = {'a': 1, 'b': 2, 'c': 3}
>>> max(my_list, key=my_list.get)
'c'
Let me know if there are any other methods.
Try this
dct={'a':3 ,'b':4 ,'c':7 ,'d':6 ,'e':8}
for i, j in dct.iteritems():
if j == max(dct.values()):
print i
From this question, there seems to be a way to retrieve the name of your variable (see caveat below before using):
a = 3
b = 4
c = 7
d = 6
e = 8
m = max(a, b, c, d, e)
for k, v in list(locals().items()):
if v is m:
v_as_str = k
break
v_as_str
output:
'e'
Caveat:
This may work if you can be sure that there are no other (unrelated) variables with the same value as the max of a to e
somewhere in your code, and appearing earlier in the locals.
Thanks to #D.Everhard & #asongtoruin in the comments

Python: how to display all possible cases to place brackets [closed]

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 6 years ago.
Improve this question
Given a number of brackets pairs. I want to display all correct combinations of these brackets. By correct I mean each bracket should be opened before closing in each combination. For example, if the number of brackets is 2 the output should be:
(())
()()
For 3:
((()))
()()()
(()())
(())()
()(())
The order of the output lines doesn't matter.
How can I do it with Python?
Try this code, please:
def __F(l, r, pref):
if r < l or l < 0 or r < 0:
return
if r == 0 and l == 0:
print(pref)
return
__F(l - 1, r, pref + "(")
__F(l, r - 1, pref + ")")
def F(n):
__F(n, n, "")
F(2)
F(3)

Categories