当我执行我的 python 文件时它不起作用 (openpyxl)

问题描述 投票:0回答:0

我正在用 Python 编写一个登录系统,它突然停止工作,我可以将内容写入其中并单击 enter,但是说

{user}@{name}-MPB {folder name} %
的部分不见了。我必须退出并返回终端再试一次。我相信 openpyxl 是问题所在,因为当我摆脱它时,它会导致代码正常工作(不保存注册)。我在 macOS Ventura、python 版本 3.11.3 和 pip 版本 23.1.

(有随机字母用于演示我能做什么)

python3 main.py
hj

k
class bcolors:
    HEADER = '\033[95m'
    OKBLUE = '\033[94m'
    OKCYAN = '\033[96m'
    OKGREEN = '\033[92m'
    WARNING = '\033[93m' # Yellow
    FAIL = '\033[91m'
    ENDC = '\033[0m'
    BOLD = '\033[1m'
    UNDERLINE = '\033[4m'

from openpyxl import load_workbook

workbook = load_workbook(filename="Test.xlsx")

sheet = workbook.active

logged_users = {

}
value = 2;
while value != sheet['C1'].value:
    logged_users.update({sheet[f'A{value}'].value: sheet[f'B{value}']})

import datetime

birthmonth = None
birthday = None
birthyear = None

def ynn():
    yn = input(f'{bcolors.WARNING}Are you going to login or signup into your account? {bcolors.ENDC}')

    if yn == 'login':
        login()
    elif yn == 'signup':
        signup()
    elif yn == 'exit':
        exit()
    else:
        print(f'{bcolors.FAIL}Please answer with either "login" or "signup".{bcolors.ENDC}')
        print('')
        ynn()

def signup():
    username = input(f'Username: ')
    if not username:
        print(f'{bcolors.FAIL}Please enter what you want your username to be.{bcolors.ENDC}')
        signup()
    print('')

    def ffirstname():
        firstname = input('First Name: ')
        if not firstname:
            print(f'{bcolors.FAIL}Please enter your first name.{bcolors.ENDC}')
            ffirstname()

        def llastname():
            lastname = input('Last Name: ')
            if not lastname:
                print(f'{bcolors.FAIL}Please enter your last name.{bcolors.ENDC}')
                llastname()

            print('')
            print(f'{bcolors.OKGREEN}Birthdate:{bcolors.ENDC}')
            def bbirthmonth():
                try:
                    birthmonth = int(input('Month: '))
                except ValueError:
                    print(f'{bcolors.FAIL}Please enter your birth month as an integer between 1-12.{bcolors.ENDC}')
                    bbirthmonth()
                else:
                    if birthmonth < 1 or birthmonth > 12:
                        print(f'{bcolors.FAIL}Please enter your birth month as an integer between 1-12.{bcolors.ENDC}')
                        bbirthmonth()
                def bbirthday():
                    try:
                        birthday = int(input('Day: '))
                    except ValueError:
                        print(f'{bcolors.FAIL}Please enter your birth day as an integer between 1-31.{bcolors.ENDC}')
                        bbirthday()
                    else:
                        if birthday < 1 or birthday > 31:
                            print(f'{bcolors.FAIL}Please enter your birth day as an integer between 1-31.{bcolors.ENDC}')
                            bbirthday()

                    def bbirthyear():
                        try:
                            birthyear = int(input('Year: '))
                        except ValueError:
                            print(f'{bcolors.FAIL}Please enter your birth year as an integer between 0-{datetime.date.today().year}.{bcolors.ENDC}')
                            bbirthyear()
                        else:
                            if birthyear < 0 or birthyear > datetime.date.today().year:
                                print(f'{bcolors.FAIL}Please enter your birth year as an integer between 0-{datetime.date.today().year}.{bcolors.ENDC}')
                                bbirthyear()

                        if birthyear < datetime.date.today().year - 18 or birthyear == datetime.date.today().year - 18 and birthday <= datetime.date.today().day and birthmonth <= datetime.date.today().month:
                            print(f'Hello {firstname} {lastname}! I see you are over 18 years old! Thanks for making a new account.')
                        else:
                            print(f'Hello {firstname} {lastname}! I see you are under 18 years old! Thanks for making a new account.')
 
                        workbook = load_workbook(filename="Test.xlsx")

                        sheet = workbook.active

                        #modify the desired cell
                        sheet[f"A{sheet['C1'].value}"] = username
                        sheet[f"B{sheet['C1'].value}"] = f'{firstname} {lastname}'

                        sheet['C1'] = sheet['C1'].value + 1
                        
                        #save the file
                        workbook.save(filename="Test.xlsx")

                        logged_users.update({username: f'{firstname} {lastname}'})

                        ynn()
                    bbirthyear()
                bbirthday()
            bbirthmonth()
        llastname()
    ffirstname()

def login():
    username = input('What is your username: ')
    name = logged_users.get(username)
    print(f'Hi {name}')
ynn()

我试过为 openpyxl 添加和删除 close() 语句。我什至尝试在命令行上编写 exit() 无济于事。

python terminal pip openpyxl macos-ventura
© www.soinside.com 2019 - 2024. All rights reserved.