Reputation: 289
I am a complete beginner in python as well as this website.
Background: I have tried to program a random password generator that allows the user to input the length of the password and how many passwords the user wants.
Everything works perfectly fine when I run the program in pharm. So I converted the script into .exe file. It does not crash instantly, it stills allow the user to input values but once the user entered the values in, it crashes.
I tried using pyinstaller from youtube tutorials to "properly" convert the script into .exe, but the same result still occurs. (Previously, I simply copy and paste my script into notepad and name that notepad in terms of .py and run it.)
Here are my codes:
import random
import sys
chars = "abcdefghijklnmopqrstuvwxyzABCDEFGHIJKLNMOPQRSTUVWXYZ0123456789!@#$%^&*+="
try:
length_password = int(input("Enter the length of your password: "))
how_many = int(input("Enter how many passwords you want: "))
except:
print("Invalid input (numbers only!)")
sys.exit()
def length_function(length):
password = ""
for number_times in range(0, length_password):
password = password + random.choice(chars)
return password
print("Here are your passwords: ")
number_times1 = 1
while number_times1 <= how_many:
print(length_function(length_password))
number_times1 = number_times1 + 1
Like I mentioned before, it runs well in pycharm but not in a .exe file.
Upvotes: 0
Views: 426
Reputation: 1512
I've seen your other question and try to explain more on the solution for this problem:
When running a code, program, or whatever, that code might end somewhere, where nothing has to be done anymore.
That's the point where your operating system (Windows, Linux etc) just exits the program. What you might expect is that the console stays open so you can read the ouput, right?
So why does this often work but often doesn't?
When calling your code from an already existing command window (for e.g. cmd.exe), the console (which is a program itself) is not finished and won't close after runnning your program. That's the point where you can read the printed output from you own program.
When you call your program from somewhere else, it is opening a console just for that purpose and it's configured in that way, that it doesn't stay open after executing your program. So when your code is executed, it prints something on the console; but immediately after its done and the console closes, too.
That is where the solution input("Press Enter to exit")
comes into play. input()
is a function which waits for user input; so it's waiting infinitely for you to hit enter. That means, your program is not finished. That gives you the time you read the output you want to read.
Upvotes: 2