Python Forum
Tuple comprehension to for loop help
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Tuple comprehension to for loop help
#1
Hello, I am currently taking a python course online. My class recently went over making a password checker api. If you take a look below, there is def get_password_leaks_count(hashes, hash_to_check).
How would I turn that tuple comprehension/generator expression into a for loop instead? I've been trying to figure out for the past 2 days and it just seems that I cannot get it right. I have rewatched the videos from the course, but I still cannot figure it out. Can someone show me what the answer would look like?

import requests
import hashlib
import sys

password_input = input("Please type in a password you would like to check ").split()

def request_api_data(query_char):
    url = "https://api.pwnedpasswords.com/range/" + query_char
    res = requests.get(url)
    if res.status_code != 200:
        raise RuntimeError(f'Error fetching: {res.status_code}, '
                           f'check the API and try again.')
    return res

def read_res(response):
    print(response.text)

def get_password_leaks_count(hashes, hash_to_check):
     hashes = (line.split(':') for line in hashes.text.splitlines())
     print(hashes)
     for h, count in hashes:
         if h == hash_to_check:
             return count
     return 0
     print(h, count)

def pwned_api_check(password):
    sha1password = hashlib.sha1(password.encode('utf-8')).hexdigest().upper()
    # Check password if it exists in API response
    first5_char, tail = sha1password[:5], sha1password[5:]
    response = request_api_data(first5_char)
    print(first5_char, tail)
    # print(response)
    # return read_res(response)
    return get_password_leaks_count(response, tail)


def main(args):
    for password in args:
        count = pwned_api_check(password)
        if count:
            print(f'{password} was found {count} times.'
                  f' Consider changing your password.')
        else:
            print(f'{password} was NOT found.')
    return "complete"
if __name__ == '__main__':
    sys.exit(main(password_file))
    # sys.exit(main(sys.argv[1:])
    # sys.exit(main(password_input))
Yes, I know there should not be same variable names, but this is just to show what I was trying to do.
My attempt:
def get_password_leaks_count(hashes, hash_to_check):
    for line in hashes.text.splitlines():
        hashes = line.split(':')
        # for h, count in hashes:
        #     if h == hash_to_check:
        #         return count
        # return 0
Reply


Messages In This Thread
Tuple comprehension to for loop help - by jsuh21 - Aug-31-2020, 05:55 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
Question mypy unable to analyse types of tuple elements in a list comprehension tomciodev 1 521 Oct-17-2023, 09:46 AM
Last Post: tomciodev
  For Loop with List Comprehension muzikman 25 6,877 Dec-18-2020, 10:45 PM
Last Post: muzikman
  code with no tuple gets : IndexError: tuple index out of range Aggam 4 2,892 Nov-04-2020, 11:26 AM
Last Post: Aggam
  Help: list comprehension for loop with double if statement mart79 3 2,484 May-04-2020, 06:34 AM
Last Post: buran
  List comprehension. Shor for loop version dervast 1 1,626 Dec-11-2019, 12:34 PM
Last Post: perfringo
  Arrange list of tuple using loop batchenr 7 3,547 Jun-16-2019, 03:24 PM
Last Post: Abdullah
  How to get first line of a tuple and the third item in its tuple. Need Help, Anybody? SukhmeetSingh 5 3,291 May-21-2019, 11:39 AM
Last Post: avorane

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020