Python Scripts

Brute Force Demonstration

This code picks a random number between 0000 and 9999, then it trys to guess it with random integers. Read more in the notes of the script!

Try it out! Download
import random #imports the library "random"
from time import sleep #imports the file "sleep" from the library "time"
global Password #makes the value "Password" able to be accessed anywhere in the code
global Trial #makes the value "Trial" able to be accessed anywhere in the code
Password = (f'{random.randrange(1, 10**4):04}') # defines the value "Password" as a random 4 digit integer, such as 2872. If the random number is below 1000, then it will add the nessecary number of zeroes before it to make it 4 digits,
# ex. the random number is 456, it would be outputted as 0456. 
#if you wanted the password to be something specific replace (f'{random.randrange(1, 10**4):04}') with a value, or an input for the user.
Trial = ' ' # defines the value trial as nothing
counter=0 # sets the counter to 0
while Trial != Password: # while the value "Trial" is not (!=) equl to the value of "Password" it will do the following
    
    Trial = str(random.randint(0,9999)) # sets the value "trial" to a random integer between 0 and 9999        you may note i could have used (f'{random.randrange(1, 10**4):04}') to make a random integer with 4 digits every time, but
    #                                                                                                          I wanted to try doing it the long way. you could replace all the ifs and elses with (f'{random.randrange(1, 10**4):04}').
    
    if int(Trial) < 1000: # If the value "Trial" (now set to intiger format) is below 1000, ie. 999,998,997,etc. it will do the following
        
        Trial = format(int(Trial), '04d') # sets the value "trial", to a state where it is four digits ('04d', 04 being four zeroes, and using zeroes to fill in the spaces)
        
        print(format(int(Trial), '04d')) # prints the now 4 digit intiger "Trial"
        
    else: # if the value "Trial" is already 4 digits it will do the following
        
        print(Trial) # prints the value "trial"
        
        counter = counter+1 # sets the intiger "counter" to one digit higher than it was before
        
        if Trial == Password: # if values "Trial" and "Password" are the same, it will do the following
        
            print() # prints a new blank line
            
            print('The password is: '+Password) # prints "The password is 1234", 1234 being an example for the variable "Password" that we determined previously
            
            print() # prints a new blank line
            
            print("It took "+ str(counter)+ " trys to guess the password") # prints "It took 12345 trys to guess the password" 12345 being an example of the variable "counter" that we determined previously
            
            print() # prints a new blank line
            
            sleep(10000) # lets the user see results by waiting 10000 units
# Notes:
# Created By Elliot C. age 14 (thats why the script is overcomplicated and could be simplified).
# script could be altered so that a specific password could be used as noted in script.
# easily could be typed out rather than printed into console, using the package pynput. this could be done by doing the following.
#
# from time import sleep
# from pynput.keyboard import Key, Controller
# keyboard = Controller()
# 
# keyboard.type(Trial)
# keyboard.press(Key.enter)
# sleep(7)
# and repeat. The program would not be able to determine when to stop, and would take way more time but that could be fixed with some tinkering.

Calculator

This bit of code was one of my very first experiences with python, no extra packages are nessecary. You can simply choose a number, then do some math!

Try it out! Download
def main():
    print("1 = Multiplication, 2 = Addition, 3 = Subtraction, 4 = Division")
    choice = input("Choice = ")
 
    if choice == "1":
        multiply()
    if choice == "2":
        add()        
    if choice == "3":
        subtract()           
    if choice == "4":
        divide()        
             
def multiply():
 
    number1 = input("Enter a number ")
    number2 = input("Enter a second number ")
    sol=float(number1)*float(number2)
    print("The multiplied numbers are "+ str(sol))
    return 0           
 
def add():
 
    number1 = input("Enter a number ")
    number2 = input("Enter a second number ")
    sol=float(number1)+float(number2)
    print("The sum of the numbers are "+ str(sol))
    return 0 
 
 
def subtract():
 
    number1 = input("Enter a number ")
    number2 = input("Enter a second number ")
    sol=float(number1)-float(number2)
    print("The subtracted total is "+ str(sol))
    return 0 
 
 
def divide():
 
    number1 = input("Enter a number ")
    number2 = input("Enter a second number ")
    sol=float(number1)/float(number2)
    print("The divided total is "+ str(sol))
    return 0
          
		  
if __name__ == "__main__":
    main()

Domino#3868

14s0c@protonmail.com