一个创建帐户的程序

时间:2019-06-20 19:21:16

标签: python

我希望程序执行的操作是要求用户首先输入年龄。如果用户年龄在18岁以上,则下一步是询问用户他/她是否拥有帐户(现在答复应该为否)。然后,应要求用户输入用户名和密码,然后将其存储在已经包含一些值的字典中,最后一步是打印出字典的值。

下面的代码详细显示了我尝试过的内容。

#Dictionary containing user info
userdata = {"therealchacha":1234, "swampout":1111, "indiejones":2222,}
#input asking for age
actAge = int(input("Enter age: "))
#function that asks for age 
def ObtainAge(age):
    if age < 18:
        return "Access not granted"
    else:
        return "Access granted"
print(ObtainAge(actAge))
#input to take in account info
accountDetails = input("Do you have an account? Type Y for yes and N for 
no.")
accountDetails = accountDetails.lower()
#function that creates account
def accountAccess(acc):
    if acc == "N":
        return "Create account"
        usrName = input("Create username: ")
        passWord = input("Create passWord: ")
        userdata.update({usrName:passWord})
        return userdata
    else:
        return "site in progress"
print(accountAccess(accountDetails))

我没有收到任何错误消息。主要问题似乎是该程序没有注意到“ If”语句,而没有将输出显示为“ Site in progress”(仅在用户输入其拥有现有帐户时才发生)。

2 个答案:

答案 0 :(得分:1)

#Dictionary containing user info
userdata = {"therealchacha":1234, "swampout":1111, "indiejones":2222,}
#input asking for age
actAge = int(input("Enter age: "))
#function that asks for age 
def ObtainAge(age):
    if age < 18:
        return "Access not granted"
    else:
        return "Access granted"
print(ObtainAge(actAge))
#input to take in account info
accountDetails = input("Do you have an account? Type Y for yes and N for 
no.")

#accountDetails = accountDetails.lower()
#function that creates account

def accountAccess(acc):
    if acc == "N":
        print("Create account")
        usrName = input("Create username: ")
        passWord = input("Create passWord: ")
        userdata.update({usrName:passWord})
        return userdata
    else:
        return "site in progress"
print(accountAccess(accountDetails))

当程序进入if条件时,您将立即返回,并且N执行n时也将转换为accountDetails = accountDetails.lower()(注释了该部分)。

我添加了print()来显示帐户创建过程。

希望对您有帮助。

答案 1 :(得分:-2)

#Dictionary containing user info
userdata = {"therealchacha":1234, "swampout":1111, "indiejones":2222,}
#input asking for age
actAge = int(input("Enter age: "))
#function that asks for age 
def ObtainAge(age):
    if age < 18:
        return "Access not granted"
    else:
        return "Access granted"
print(ObtainAge(actAge))
#input to take in account info
accountDetails = input("Do you have an account? Type Y for yes and N for no.")
accountDetails = accountDetails
#function that creates account
def accountAccess(acc):
    if acc == "N":
        return "Create account"
        usrName = input("Create username: ")
        passWord = input("Create passWord: ")
        userdata.update({usrName:passWord})
        return userdata
    else:
        return "site in progress"
print(accountAccess(accountDetails))

经过编辑,可修复OP从未询问过的问题,并使该任务看起来很有意义。如果我猜测您的意思是正确执行操作,那么您的代码应该看起来像这样。

class user_setup():
    def __init__(self, existing_user_data):

        self.user_data = existing_user_data
        self.hasAcc = False
        self.access = False
        self.accinit = False

    def _account_init(self):
        age = int(input('Enter your age: '))
        if age < 18:
            print ('You are too young to setup an account')
        else:
            self.access = True

        if self.access:
            haveAccount = input('Do you have an account? Type Y for yes and N for no: ').upper()
            if haveAccount != 'N' and haveAccount != 'Y':
                print('\nYou must specify Y for yes or N for no')
                self._account_init()

            self.accinit = True
            if haveAccount == 'Y':
                self.hasAcc = True
            else:
                self.hasAcc = False

    def setup_account(self):
        if not self.accinit:
            self._account_init()
        if self.hasAcc and self.access:
            print ('Site in Progress')
        elif self.access and not self.hasAcc:
            print ('Please follow the following prompts to create an account\n')
            usrName = input('Please enter a username: ')
            passWord = input('\nPlease choose a password: ')
            verify = input('\nPlease verify your password: ')

            if passWord != verify:
                print ('passwords did not match\n')
                self.setup_account()  
            else:
                self.user_data[usrName] = passWord
                return None


#Dictionary containing user info
userdata = {"therealchacha":1234, "swampout":1111, "indiejones":2222,}

accounts = user_setup(userdata)

accounts.setup_account()