0
Range fuction not working?
Hi there, so I've made a code that seems to work fine on python3 and python after few changes, but in app here it crashes... Can you tell me why it's like this? https://code.sololearn.com/c7oHphCZ1Air/?ref=app https://code.sololearn.com/cdRaPJ0uO2Os/?ref=app
11 Antworten
+ 2
You have a bunch of invisible characters in your code, probably from copying and pasting. If you view them in code playground on PC you'll see the as highlighted red dots.
+ 3
Here the 1st with characters removed:
#random pass gen based on chosen length
#failed code - uses Python3 to work >.>
import random
import string
#taking lenght (did not manage to set checking and error check, dont know how yet :()
l=eval(input("Please type desired length of password\n"))
print ("Chosen lenght is "+str(l))
#creating generator
def rangen(size=l, chars=string.digits + string.ascii_lowercase + string.ascii_uppercase + string.punctuation):
return (''.join(random.choice(chars) for _ in range(size)))
#its for checking if password contains all needed parts (digit,symbol,character)
def valid(a=''):
nums = string.digits
alphs = string.ascii_lowercase + string.ascii_uppercase
signs = string.punctuation
if any ((c in nums) for c in a):
if any ((c in alphs) for c in a):
if any ((c in signs) for c in a):
return True
else:
return False
#running the gen
def main ():
i=1
while i>0:
passw = rangen()
valid(passw)
if valid(passw) is True:
i=0
else:
i=1
else:
print ("\nWould you kindly use this password: "+'" '+passw+' "'+" ?")
with open('pass.txt' , 'a') as file:
file.write(passw+'\n\n')
main()
+ 2
@BlugonYT here's your code cleaned up a bit with a few modifications. Take a good look at Dezgol's code for some further ideas of adding characters other than integers.
#Author : BlugonYT
#THE SPECIAL FUNCTION OF THIS CODE IS
#YOU CAN SET THE LENGTH OF THE PASSWORD THAT YOU GENERATE
import random
def check_input(num):
if num > 40: # you can change the maximum length here
print('The number is out of range')
else:
return num
inp = int(input("Enter the length of the password that you want to generate: "))
print("\nYour password is : ", end='')
[print(random.randint(1, 9), end="") for i in range(check_input(inp))]
print("\n -----------------------------------------\n All copyright reserved | Made by BlugonYT\n -----------------------------------------")
+ 2
You sure you don't know already? You were already using it lol. end sets the print functions terminating character(s). By default it is set to '\n' for a newline, but you can change it to any character(s) you wish. In this case an empty string so that the next thing printed will be on the same line directly following the last character of the string that had just been printed. You used it so that the random numbers were output next to each other on the same line to make them look as though they are all the same number.
+ 2
print("\nYour password is: ", end='')
I used it on this line so that when the password is output it will be on the same line instead of a newline.
output looks like:
Your password is: 73719263839
instead of:
Your password is:
73719263839
It just looks at bit cleaner IMO.
This part uses pythons list comprehension.
[print(random.randint(1, 9), end="") for i in range(check_input(inp))]
it does the same as:
for i in range(check_input(inp)):
print(random.randint(1, 9), end='')
+ 1
@ChaoticDawg woha, ok - i did not notice them. obviously :D now it works fine, I'm really thankful for the help!
0
I got a code similar to this one..
https://code.sololearn.com/cR5LH0EShEFi/?ref=app
0
@ChaoticDawg can you help to enhance my code?
0
@ChaoticDawg what is the use of "end="
0
Sorry.. I forgot that i used it... Someone actually explained it before.. I just don't understand why you use it at here..
print("\nYour password is : ", end='')
[print(random.randint(1, 9), end="") for i in range(check_input(inp))]
I don't quite understand this part though...
0
Thanks again... 😁😁
Now i seriously understand how it works already