Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Noob help :)
#1
Hi all. I'm getting an error message when running the following code.
I'm assuming its something to do with integers not being strings and vice-versa.

User chooses whether the 10 passwords to be generated should be made from Upper/Lower/Mixed chars.

import string

from random import *

generated = 0
min_char = 8
max_char = 12
optionchar = 0
chosenchar = ""

while optionchar != '1' and optionchar != '2' and optionchar != '3':
    print('Choose the string type for the password:')
    print('1 - lowercase')
    print('2 - UPPERCASE')
    print('3 - MiXeDcAsE')
    optionchar = input()


print('You chose ' + str(optionchar))


if optionchar == 1:
    chosenchar == str(string.ascii_lowercase)
if optionchar == 2:
    chosenchar == str(string.ascii_uppercase)
if optionchar == 3:
    chosenchar == str(string.ascii_letters)



while generated <10:
    password = "".join(choice(str(chosenchar)) for x in range(randint(min_char, max_char)))
    print ("This is your password: ",password)
    generated = generated + 1
thank you
Reply
#2
You are correct
optionchar = input()
At this point optionchar is a string, so you can do something like:
optionchar = int(input())
In future when you are not sure, simply use print for debugging. If you want to know a type of a variable, you can print it with:
print(type(optionchar))
Edit:

Unless there some other purpose I don't see, you don't even need to convert input to integer, if you simply compare to string in the first place:
if optionchar == '1':
Reply
#3
Thank you, that worked.
Reply


Forum Jump:

User Panel Messages

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