May-09-2018, 05:45 AM
Hello all,
I have made a simple password generator but i'm having an issue with one part of the script. On line 59 there is an else statement which I thought would attach to the very first if statement on line 19 but whenever I input anything but a positive integer into the script it gives me an error stating
"Traceback (most recent call last):
File "PasswordGenV2.py", line 59, in <module>
pwdgen()
File "PasswordGenV2.py", line 17, in pwdgen
length = int(raw_input("How many characters do you want in your password? "))
ValueError: invalid literal for int() with base 10: 'd'"
I guess im not too sure what the error message is telling me as I am pretty new to Python as a whole. Any help would be appreciated. Below is the code I have so far.
I have made a simple password generator but i'm having an issue with one part of the script. On line 59 there is an else statement which I thought would attach to the very first if statement on line 19 but whenever I input anything but a positive integer into the script it gives me an error stating
"Traceback (most recent call last):
File "PasswordGenV2.py", line 59, in <module>
pwdgen()
File "PasswordGenV2.py", line 17, in pwdgen
length = int(raw_input("How many characters do you want in your password? "))
ValueError: invalid literal for int() with base 10: 'd'"
I guess im not too sure what the error message is telling me as I am pretty new to Python as a whole. Any help would be appreciated. Below is the code I have so far.
#! /usr/bin/env python #imports random to be used to pick random characters for the password import random #imports strings to be used to pick from randomly import string #sets variables to be called in the random.choice function below #strong password uses x x = (string.uppercase + string.lowercase + string.digits + string.punctuation) #weak password uses y y = (string.lowercase + string.digits) #starting the main loop def pwdgen() : print "I heard you were looking for a password generator..." while True: #asks how long you want the password to be length = int(raw_input("How many characters do you want in your password? ")) #checks to see if the input for number of characters is an integer if (isinstance(length, int)) == True: #asks what strength you want ie strong or weak strength = raw_input("Do you want a strong password or a weak one? ") print "\n" #defines the password generation for strong passwords if strength.lower() == "strong": passwordstrong = ''.join([random.choice(x) for _ in range( int(length))]) print passwordstrong print "\n" print "Don't forget to copy your new password!" print "\n" #defines the password generation for weak passwords elif strength.lower() == "weak": passwordweak = ''.join([random.choice(y) for _ in range( int(length))]) print "\n" print passwordweak print "\n" print "Don't forget to copy your new password!" print "\n" #defines what happens when any other input is put in to the strength question else: print ("Sorry, I don't understand what you said.") print "\n" #asks if the person needs another password startagain = (raw_input("Do you need another password? ")) #if answer is yes if startagain.lower() == "yes": #restart the loop continue elif startagain.lower() == "no": #if answer is no, stop the loop and end the program break else: print "Sorry, I don't understand what you said." print "\n" break else: #if the input from line 19 isn't an integer print "Sorry, please enter a positive integer." pwdgen()