Home > database >  SyntaxError: invalid syntax - error with if statement in python
SyntaxError: invalid syntax - error with if statement in python

Time:07-28

I'm a semi-beginner with Python and I was wondering if anyone could help me with an issue i'm receiving?

SyntaxError: invalid syntax

This is on the line "if generated_characters != < 100:"

I was wondering how I would write this to say if the answer to generated_characters is over 100 to ask the user to choose a password amount within 1-99 and repeat the original question.

I presume there are also some other errors in this code that I have made - it would be appreciated if anyone could point these out but I am more than happy to try and resolve by myself as it's probably only an indentation error or typo.

Also I apologize if the code is inefficient, I am not very experienced in programming or Python in general.

Here is the code:

import random
import os

print("Welcome to your password generator! \n")

clear = ('cls' if os.name == 'nt' else 'clear')

chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890!£$%&^*()@"


def characters():

    generated_characters = int(input(
        f"How many characters would you like your {passwords_amount} passwords to be? "))

    if generated_characters != < 100:
        os.system(clear)
        print(
            "For efficiency purposes, please choose a character amount that's 99 or less.")
        characters()
    else:
        os.system(clear)

        for num in range(passwords_amount):
            password = ""
            for chars in range(generated_characters):
                password  = random.choice(chars)
            print(password)


passwords_amount = int(
    input("How many passwords would you like to generate? "))

characters()

Thanks in advance!

CodePudding user response:

If you logically think about it the equivalent statement to "not less than 100" would be "if greater than 99". So this should give you what you are after.

if generated_characters > 99:

Hope that helps.

Regards.

CodePudding user response:

this should run pretty smoothly. Also noticed that when you use password = random.choice(chars) you're only getting digits returned. Use password = random.choice(string.ascii_letters string.digits) instead.

See below for entire functioning script:

import random
import os
import string
print("Welcome to your password generator! \n")
clear = ('cls' if os.name == 'nt' else 'clear')
chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890!£$%&^*()@"


def characters():
    passwords_amount = int(
    input("How many passwords would you like to generate? "))
    
    generated_characters = int(input(
        f"How many characters would you like your {passwords_amount} passwords to be? "))

    if generated_characters >= 100:
        print(
            "For efficiency purposes, please choose a character amount that's 99 or less.")
        while generated_characters >= 100:    
            generated_characters = int(input(
                f"How many characters would you like your {passwords_amount} passwords to be? (keep below 100!) "))
            if generated_characters <=99:
                for num in range(passwords_amount):
                    password = ""
                    for chars in range(generated_characters):
                        password  = random.choice(string.ascii_letters   string.digits)
                    print(password)
    else:
        os.system(clear)

        for num in range(passwords_amount):
            password = ""
            for chars in range(generated_characters):
                password  = random.choice(string.ascii_letters   string.digits)
            print(password)



characters()

Hope that helps.

  • Related