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
I have been trying to make a simple py script that will check if the result is different. It should check for password and one number from 0-100, and there are 3 possible answers:Wrong, Try again
and the last one which I need(and don't know it, so I am trying to pull that out).
This is intended to be like a script for brute forcing.
p="Wrong!"
a="Try again."
for i in range(00, 100):
print("SuperPassword " + str(i))
if not(p) and not(a):
break
print i
break
else:
continue
It just prints all numbers and doesnt stop till it end(it should stop at right digit password), and how to tell python to print 00 as password since it prints 0 no matther what I type(for e.g. for i in range(00,100) it prints 0,1,...)
Thanks in advance
I think you're looking for a nested for loop...
def brute_force():
for x in range(10):
for y in range(10):
p = '%d%d' % (x, y)
print p
if p == '87':
print 'Right!'
return
print 'Wrong!'
given that we have little information on the question. I suggest that you start with this.
p = 0
for i in range(0, 100):
print("SuperPassword " + str(i))
if p == i:
break
you should be comparing p (i guess that it is password) with i.
Related
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
I'm trying to make a run length decoder that doesn't use 1s. For example a string that could be passed through would be something like ''' A2C3GTA'''. I made what i thought would work and am having trouble finding where I went wrong. I'm a beginner to python so I am sorry for the simple question. Thank you!
def decode(compressed):
decoded= ""
count = 0
for x in compressed :
if x.isdigit():
count += int(x)
y = compressed
decoded += y[int(x)+1] * count
count = 0
else :
decoded += x
print (decoded)
When you find a number-letter pair, you fail to skip the letter after you expand the pair. This is because you used a for loop, which is a more restrictive structure than your logic wants. Instead, try:
idx = 0
while idx < len(compressed):
char = compressed[idx]
if char.isdigit():
# replicate next character
idx += 2
else:
decoded += char
idx += 1
That will take care of your iteration.
Your in appropriate replication, the 22 in your output, this comes from an incorrect reference to the position:
decoded += y[int(x)+1] * count
Here, x is the run length, not the position of the character. If the input were A7B, this ill-formed expression would fault because of an index out of bounds.
In the code I gave you above, simply continue to use idx as the index.
I trust you can finish from here.
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
I'm trying to make an average but for some reason when I try to make one it doesn't work.
I have global variables and array defined at the begining of my document :
vent_moyenne_km = []
compteur_moyenne=0
I have one of my function that is called every X time. In that one, I calculate a velocity with some value that are display on a label of my interface. that part is working, but not the mean
global compteur_moyenne
compteur_moyenne += 1
ventkmh = (vent_1[3][0]*256 + vent_1[4][0]) /100 *3.6
label_vent2_2.config(text= "%.2f" % ventkmh)
vent_moyenne_km.append("%.2f" % ventkmh)
vent_1.clear()
if compteur_moyenne == 5:
compteur_moyenne = 0
print(vent_moyenne_km)
label_vent4_2.config(text=statistics.mean(vent_moyenne_km))
vent_moyenne_km.clear()
of course in my imports I have :
import statistics
When I comment the line label_vent4_2.config(text=statistics.mean(vent_moyenne_km)), everything works and I see in the terminal my array with 5 values. I also tried numpy and even tried to make a for items in array: then add then manually, and everytime I get the error : class 'IndexError'
I'm really not sure how to fix that.
For calculating an average of a list just use numpy:
def function():
value = random.randint(0,1)
return value
list = []
for i in range(100):
list.append(function())
if i%5 == 0:
print(np.average(list))
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 5 years ago.
Improve this question
I was trying to get 3 random numbers within range in python without replacement. I did this
rand_lst = []
while(True):
rand = random.randint(0, length-1 )
if(len(rand_lst) == 3):
break
if(rand not in rand_lst):
rand_lst.append(rand)
This code is inside a for loop. This returns only 0 and thus an infinite loop. I also tried numpy.random.randint but some how random is keeping track of previously generated numbers and I endup with value error.
Trying putting print(length) right before your while True. You'll almost certainly find that the value of length is not what you expect it to be. (It's probably 0 or 1, when you want it to be 3 at least.)
if i understand you correctly,
you want something like this:
import random
rand_lst = []
length = 10
while(True):
rand = random.randint(0, length-1 )
if(len(rand_lst) == 3):
break
if(rand not in rand_lst):
rand_lst.append(rand)
print(rand_lst)
this way "length" has a value and will not throw an error
Explanation: https://stackoverflow.com/a/33806926/8386640
Use random.sample. Here is code.
import random
rand_lst = []
length = 3
while(len(rand_lst) < 3):
rand_lst.append(random.sample(range(length), 3))
print rand_lst
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 5 years ago.
Improve this question
Here is my homework:
Write a function that repeatedly generates random integers in the range [0,n[, until two consecutively generated numbers are identical.then return the number of generated numbers.
Here is what I did:
def function(n):
for i in range(100):
random.randint(0,n)
and this outputs a hundred numbers, however I need it to stop when it detects two identical numbers, how is that done?
For this you could use two names: one for the newly generated random number, and one for the previous one.
You need also to make your loop differently, as you don't know how long it needs to iterate before getting a match:
import random
def function(n):
a = -2
b = -1
i = 0
while a != b:
a, b = random.randint(0,n), a
i += 1
return i
# Sample run
times = function(400)
print (times)
See it run on repl.it
To do it without assigning a and b strange values, use an infinite while loop, then break when the values are the same. This is similar to #trincot's solution:
import random
def function(n):
a = None
i = 0
while True:
a, b = random.randint(0,n), a
i += 1
if a == b:
return i
# Sample run
times = function(400)
print(times)
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 6 years ago.
Improve this question
Hi all I have read through the previous answers to this question and can get the code to run. What I want to understand is why my code doesn't run.
Thanks
def collatz(number):
if number % 2 == 0:
return number // 2
elif number % 2 == 1:
return 3 * number + 1
print('Enter a number')
number = int(input())
while number != 1:
print(int(collatz(number)))
You are not updating number in your while loop so you are stuck in an infinite loop.
You should assign return value of collatz to number back, to update number.
while number != 1:
number = collatz(number)
print(number)