Python Forum
Not sure how to fix this loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Not sure how to fix this loop
#1
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.


#! /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()
Reply
#2
You have pretty much figured what the problem is. On line 17, you convert your input to an integer with length = int(. But strings cannot be converted to integer. So the error happens before the execution reaches if statement. Removing int() (the conversion) might do the trick. Then it's only a matter of how you want floats to be handled, if user inputs them.
Reply
#3
In error you get, is an information, that you tried to give "d" in a command line, and convert it to an integer. That, of course, can't work. Maybe would be some idea to make some small check what you get in raw_input. You can do it using "isdigit" method. as you can see below, it returns true when they are only numbers in the string. Btw. you should think about checking the length of "length" because some truly huge number can crash your code.

x = '123'

x.isdigit()
True

y = 'as'

y.isdigit()
False

xyz = '1.123'

xyz.isdigit()
False

xneg = '-5'

xneg.isdigit()
False
Reply
#4
Thanks you guys! I think I know where to go from here.
Reply


Forum Jump:

User Panel Messages

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