J Walia
J Walia

Reputation: 11

Variable Not Defined After Conditions

This is supposed to be a passcode generator program in Python 3.

import string
from random import *

print("Type Yes or No For The Following Questions:")

letters = raw_input("Do you want letters in your passcode?" )
if raw_input == 'Yes': chars1 = string.ascii_letters 
elif raw_input == 'No': chars1  = ""

digits = raw_input("Do you want digits in your passcode?" )
if raw_input == 'Yes':chars2 = string.digits
elif raw_input == 'No': chars2 = ""

symbols = raw_input("Do you want symbols in your passcode?" )
if raw_input == 'Yes': chars3 = string.punctuation 
elif raw_input == 'No': chars3  = ""

requestedlength = input("What passcode length do you want? Type any   number: ")
length = int(requestedlength)

chars = chars1 + chars2 + chars3

passcode = raw_input("Type Enter To Generate Random Passcode: ")
print("".join(choice(chars) for x in range((length))))

What am I doing wrong here? The error states that chars1 chars2 and chars3 are not defined. How would I define those variables after they get altered in the conditional statements? I'm pretty new to Python so I apologize if the code is a mess :(

EDIT: Thank you all for the answers!

Upvotes: 0

Views: 40

Answers (3)

vmf91
vmf91

Reputation: 1937

This is how I get it to work:

import string
from random import *

print("Type Yes or No For The Following Questions:")

letters = input("Do you want letters in your passcode?" )
if letters == 'Yes': chars1 = string.ascii_letters 
elif letters == 'No': chars1  = ""

digits = input("Do you want digits in your passcode?" )
if digits == 'Yes':chars2 = string.digits
elif digits == 'No': chars2 = ""

symbols = input("Do you want symbols in your passcode?" )
if symbols == 'Yes': chars3 = string.punctuation 
elif symbols == 'No': chars3  = ""

requestedlength = input("What passcode length do you want? Type any   number: ")
length = int(requestedlength)

chars = chars1 + chars2 + chars3

passcode = input("Type Enter To Generate Random Passcode: ")
print("".join(choice(chars) for x in range((length))))

Upvotes: 0

SuperStew
SuperStew

Reputation: 3054

Because your if statements aren't asking the right thing

You set some variable with the input

letters = raw_input("Do you want letters in your passcode?" )

and then ask if raw input is "Yes", which it never is

if raw_input == 'Yes': chars1 = string.ascii_letters 

So your if statements are always false and the variables (chars1 etc) never get set, so you get the error

To fix it, simply change your if statement

if letters == 'Yes': chars1 = string.ascii_letters 

Upvotes: 1

vs97
vs97

Reputation: 5859

Change your "input" typo in your if-statements to "letters" and etc. for other loops:

letters = input("Do you want letters in your passcode?")
if letters == 'Yes':
    chars1 = string.ascii_letters
elif letters == 'No':
    chars1 = ""

digits = input("Do you want digits in your passcode?")
if digits == 'Yes':
    chars2 = string.digits
elif digits == 'No':
    chars2 = ""

symbols = input("Do you want symbols in your passcode?")
if symbols == 'Yes':
    chars3 = string.punctuation
elif symbols == 'No':
    chars3 = ""

Upvotes: 0

Related Questions