Как найти что-то из текстового файла - PullRequest
0 голосов
/ 21 февраля 2020

Я пытаюсь создать проект контактов python, но я не знаю, как найти единственного пользователя. Я пытался найти из файла, но я не могу, любезно дать код для этой части. Если вы обнаружите какие-либо другие ошибки, пожалуйста, исправьте это тоже. Это код.

# -*- coding: utf-8 -*-
"""
Created on Wed Feb 19 17:56:45 2020

@author: Teerth Jain
"""

import time

from transpositionDecrypt import decryptMessage as dm
from transpositionEncrypt import encryptMessage as em
key = 8
users = {}
users1 = {}
print("Welcome to Teerth's Contacts Saver and Reader...")
time.sleep(0.5)
r_or_w = input("Do you want or (r)ead or (a)dd or (d)el contacts: ")
if r_or_w == 'r':
    what = input("Do you want to read a single contact(y, n): ")
    if what == 'y':
        #here is the part where you need to give me advice#


    if what == 'n':
        print("Displaying the whole list...")
        q = open('contacts.txt', 'r')
        for liness in q:
            print(dm(key, liness.strip()))



if r_or_w == 'a':
    while True:
        addwho = input("Please enter the name: ")
        number = input("Please enter the number: ")
        file2 = open('contacts.txt', 'r')
        y = dm(key, file2.read())
        if addwho in y:
            print("User in contact list, please try again")
            addwho = input("Please enter the name: ")
            number = input("Please enter the number: ")

        users1.update(addwho = number)
        file1 = open('contacts.txt', 'a')
        q = f'{addwho} : {number}'
        file1.write(em(key, q))
        file1.write("\n")
        file1.close()
        print("Number is ciphered and added.")
        again = input("Do you want to add another contact(y, n): ")
        if again == 'n':
            break

Сейчас таких ошибок нет, но если вы можете изменить код, чтобы сделать его лучше, сделайте это, спасибо

...