Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Not adding and assigning?
#1
*Newb post, once again* I was wondering why my code could not add and assign to a set variable. It seems that if the variable is outside a while True: loop, it cant add and assign. What is my mistake? I cannot seem to find what is wrong with it. Any help is greatly appreciated.
import string
import time
chars = string.printable
import sys
number1 = 0
number2 = 0
number3 = 0 
number4 = 0
number5 = 0
number6 = 0
number7 = 0
number8 = 0
number9 = 0
number10 = 0
number11 = 0
number12 = 0
number13 = 0
number14 = 0
number15 = 0
number16 = 0
number17 = 0
number18 = 0
number19 = 0
number20 = 0
number21 = 0
number22 = 0 
number23 = 0
number24 = 0
char_list1 = chars[number1][0]
char_list2 = chars[number2][0]
char_list3 = chars[number3][0]
char_list4 = chars[number4][0]
char_list5 = chars[number5][0]
char_list6 = chars[number6][0]
char_list7 = chars[number7][0]
char_list8 = chars[number8][0]
char_list9 = chars[number9][0]
char_list10 = chars[number10][0]
char_list11 = chars[number11][0]
char_list12 = chars[number12][0]
char_list13 = chars[number13][0]
char_list14 = chars[number14][0]
char_list15 = chars[number15][0]
char_list16 = chars[number16][0]
char_list17 = chars[number17][0]
char_list18 = chars[number18][0]
char_list19 = chars[number19][0]
char_list20 = chars[number20][0]
char_list21 = chars[number21][0]
char_list22 = chars[number22][0]
char_list23 = chars[number23][0]
char_list24 = chars[number24][0]
pwattempts = 0
password = input("Enter the password to crack (Between 8 and 24 characters please):")
y = len(password)
while True:
	if 7 < y < 25:
		print("You have an acceptable amount of characters.")
		break;
	else: 
		print("Error. Closure of program.")
		sys.exit(0)
#To start at 000000:
io = 0 
print("Start")
attempts = 0
while True:
	attempts = char_list1 + char_list2 + char_list3 + char_list4 + char_list5 + char_list6 + char_list7 + char_list8
	pwattempts += 1
	if attempts == password:
		io += 1
		print("The password is" + attempts)
		break;
	else:
		print(attempts)
		if number2 == 100:
			number1 += 1
			number2 = 0
			number3 = 0
			number4 = 0
			number5 = 0
			number6 = 0
			number7 = 0
			number8 = 0
		elif number3 == 100:
			number2 += 1
			number3 = 0
			number4 = 0
			number5 = 0
			number6 = 0
			number7 = 0
			number8 = 0
		elif number4 == 100:
			number3 += 1
			number4 = 0
			number5 = 0
			number6 = 0
			number7 = 0
			number8 = 0
		elif number5 == 100:
			number4 += 1
			number5 = 0
			number6 = 0
			number7 = 0
			number8 = 0
		elif number6 == 100:
			number5 += 1
			number6 = 0
			number7 = 0
			number8 = 0
		elif number7 == 100:
			number6 += 1
			number7 = 0
			number8 = 0
		elif number8 == 100:
			number7 += 1
			number8 = 0
		else:
			number8 += 1
So it doesnt give me an error code. It just prints 00000000 forever, but i want it to print 00000001, 00000002, and so on. Can anybody relate or help me? Thanks a lot. Also, if you see any other errors or have suggestions, anything is greatly appreciated. Thank you so much! Big Grin Big Grin
Reply
#2
(Oct-10-2018, 03:47 PM)00712411 Wrote: attempts = char_list1 + char_list2 + char_list3 + char_list4 + char_list5 + char_list6 + char_list7 + char_list8

So you re-assign attempts every iteration, but never change what any of the char_lists are, so attempts will always be the same.

Why would you expect attempts to change, if you're not doing anything that would change it?
Reply
#3
Using index as a part of a variable name is extremely inefficient - and un-needed - since you have lists
numbers = [0] * 24
char_list = [chars[0]] * 24
You write a lot of code that masks your intention

BTW, chars[<index>] returns a string of 1 character, second index is redundant
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#4
else:
            number8 += 1
on line 118-119, I add 1 to number 8. On line 36, I state that number 8 will define the character in the output of char_list8 as such:
char_list8 = chars[number8][0]
Is this unclear to you? Sorry, i dont mean to be rude but it seems to me that that would work. I dont see what could go wrong. Thanks again for everything,

007
Reply
#5
You shouldn't have 24 variables named number1-number24, you should have a list of 24 values. Same with char_list. You assign char_list like so: chars[number2][0]. But since chars is a string, chars[number2] returns a string of length 1. All the [0] is return the first character of a string of length one, which is the string.

You are not assigning to a set anywhere. There is no set anywhere in your code. A set is a container that has one and only one of each value in it, and provides fast access to the 'in' operator. You just have a bunch of integers and strings.

The reason it prints 0000000 forever is that you are printing attempts. That variable is the sum of the char_list variables. You never change the char_list variables, so attempts never changes, so you always print the same thing. Changing number1 does not retroactively change the assignment of char_list1 to chars[number1]. You have to reassign that if you want attempts to change.

Edit: It looks like a lot of others pointed all of this out while I was typing it up. Listen to everything they said.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#6
(Oct-10-2018, 04:27 PM)00712411 Wrote:
else:
            number8 += 1
on line 118-119, I add 1 to number 8. On line 36, I state that number 8 will define the character in the output of char_list8 as such:
char_list8 = chars[number8][0]
Is this unclear to you? Sorry, i dont mean to be rude but it seems to me that that would work. I dont see what could go wrong. Thanks again for everything,

007

You change number<index> - that does not change change char_list<index>. Your code cannot possibly work - as you can see by yourself.
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#7
(Oct-10-2018, 04:27 PM)00712411 Wrote:
else:
            number8 += 1
on line 118-119, I add 1 to number 8. On line 36, I state that number 8 will define the character in the output of char_list8 as such:
char_list8 = chars[number8][0]
Is this unclear to you? Sorry, i dont mean to be rude but it seems to me that that would work. I dont see what could go wrong. Thanks again for everything,

007

char_list8 has it's value set to whatever chars[number8][0] is, but it's value doesn't get updated if number8 happens to change. They're two unrelated things. If you want char_list8 to change, you have to also change char_list8. Programming would be incredibly difficult if all your variables changed their values whenever anything happened to unrelated other parts of your code.
Reply
#8
(Oct-10-2018, 04:35 PM)nilamo Wrote: char_list8 has it's value set to whatever chars[number8][0] is, but it's value doesn't get updated if number8 happens to change. They're two unrelated things. If you want char_list8 to change, you have to also change char_list8. Programming would be incredibly difficult if all your variables changed their values whenever anything happened to unrelated other parts of your code.

Looks like OP believes thst he performs sort of reference binding from index to character set - which may be with a proper wrapper class, but not like that.

Looks like brute-force password breaker - which, in theory, should be implmented by a recursive function. Though in Python, with itertools.product, it is rather simple in a loop
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Assigning a new value to variable uriel 1 1,570 Dec-04-2021, 02:59 PM
Last Post: Underscore
  assigning a variable :( gr3yali3n 0 1,289 Sep-22-2020, 09:02 PM
Last Post: gr3yali3n
  Assigning variables Godserena 4 2,134 Apr-26-2020, 06:59 AM
Last Post: buran
  Assigning a Variable Help MC2020 5 2,880 Jan-06-2020, 10:54 PM
Last Post: MC2020
  Adding markers to Folium map only adding last element. tantony 0 2,094 Oct-16-2019, 03:28 PM
Last Post: tantony
  Assigning an item from a list xlev 1 1,429 Sep-27-2019, 04:42 PM
Last Post: Larz60+
  assigning index 3Pinter 6 2,948 Jan-18-2019, 10:07 PM
Last Post: nilamo
  Assigning iter_row value to variable ankey 8 8,945 Sep-24-2018, 03:51 PM
Last Post: ankey
  Assigning a new variable in a IF loop pythoneer 5 3,735 Mar-02-2018, 05:21 AM
Last Post: pythoneer
  Assigning to string slice michaeljhuman 1 2,727 Feb-08-2018, 12:57 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020