Python Password Generator


#1

Hello,
I'm making a password generator. But I'm getting an error

this is the code I wrote
https://pastebin.com/AAv4t9ur

this is the error I get
line 11, in
for i in range(pw_lenght):
TypeError: 'str' object cannot be interpreted as an integer

can someone help me please?


#2

The issue is made in this line:

pw_lenght = input()

You're later trying (line 11) to run a for loop based on the number it's equal to, but the variable is equal to a string, while range() expects an integer.

To fix this, try changing this:

pw_lenght = input()

To this:

pw_lenght = int(input())

And get rid of this line (you don't need it):

pw_lenght = ''

#3

I made those changes and its not working...


print('Welcome to password generator')
print('Say yes if you want to start')
yon = ''
yon = input()
if yon == 'yes':
    print('How many letters do you want?')
    pw_lenght = int(input())
    mypw = ''
    alphabet = 'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()'
    for i in range(pw_lenght):
        next_index = random.randrange(len(alphabet))
        mypw = mypw + alphabet[next_index]
 
        print(mypw)
 
 
 
else: print('Delete my life')

#4

Errors:

Traceback (most recent call last):
  File "python", line 4, in <module>
  File "<string>", line 1, in <module>
NameError: name 'yes' is not defined

and
(this one's just hilarious)

Oops, try again. Did you create a function called is_prime? Your code threw a "global name 'is_prime' is not defined" error.

#5

And I want to use this generator so that's why I'm trying to make it work :smiley:


#6

did you import random?


#7

No :smiley: i forgot :smiley:


#8

does it work now? or does it give the same error?


#9

Where and how do I import random? I just realized that I haven't Imported anything in a REALLY LONG TIME.

import random
print('Welcome to password generator')
print('Say yes if you want to start')
yon = ''
yon = input()
if yon == 'yes':
    print('How many letters do you want?')
    pw_lenght = int(input())
    mypw = ''
    alphabet = 'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()'
    for i in range(pw_lenght):
        next_index = random.randrange(len(alphabet))
        mypw = mypw + alphabet[next_index]
 
        print(mypw)
 
 
 
else: print('Delete my life')

And yea same error


#10

Did you get it to work?


#11

yes it worked for me


#12

Did you get my code to work?


#13

which python are you using?


#14

I don't know
2.7 i think


#15

What are you using?.


#16

I'm using Codecademy's Python


#17

yes, your code worked for me. I think the problem is, you're using an old version of python the script I made is used by python 3.6. And the import needs to be at the top I think.


#18

maybe you can download and install python on python.org.


#19

Where can I download a Python 3.6 program...
What I mean to say is What program are you using?


#20

Welp that solves it I'll go download one now :smiley: