Search code examples
pythonpythonanywhere

I am trying to create a loop so that while y is smaller than x it will loop through print statements while alternating through them


my code and the error

I am trying to make a program that will create a randomized password. Along with that, it is supposed to alternate between

print(chr(letter), end="")
print(chr(number), end="")

as it makes it so that the password looks something like "A2h8l" where it alternates between numbers and letters.

import random

number = random.randint(48,57)
letter = random.randint(65,122)

print(input("How many characters do you want in your password?"))

x = input

y = int(letter + number)

while int(x > y):
    print(chr(letter), end="")
    print(chr(number), end="")

For some reason, however,

while int(x > y):

shows up with an error, and I'm not sure what to do about it. No clue what I'm doing or doing wrong, and any help is appreciated.


Solution

  • int(1 > 2) is syntactically incorrect.

    You cannot pass a comparison like this to the int() function. The correct way to do this would be

    if int(1) > int(2):
    

    Although, you still do not need the int() if you are passing an integer to begin with.


    Even when resolving this issue your code continues to have problems.

    Your loop will run indefinitely or not at all, dependant on the values of x and y, and will also print the same two chars constantly.


    A better way to handle this is to:

    1. Define password length
    2. Loop over range(length)
    3. Generate random integers and append them to a string

    import random
    length = int(input('Password Length'))
    password = ''
    
    for i in range(length):
        password = password + chr(random.randint(65,122)) + chr(random.randint(48,57))
    
    password
    #'Q5I4D8p8i9l1p7j0I6l9'