Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Missing Postional Arguments
#1
I am making an accommodation program and I ran into this error.I saw other threads with the same issue om the site but they didn't help me figure it out. I am running into the error after I search the realtor file for a match. That is the login part of the program, when a student logs in.

This is my main file:
from classes.users import Student, Realtor
import csv

# declaration of files
students_file = "students.txt"
realtor_file = "realtor.txt"


def main():
    prompt()
    option_select()


def prompt():
    print("Welcome to Mason Real Estate")
    print("Please Login or Create Account")


# selection to crete account or login
def option_select():
    print("1. Create Account \n2. Login\n")

    user_choice = int(input("What would you like to to today?: "))

    if user_choice == 1:
        create_user()
    elif user_choice == 2:
        login_user()


# choice to create student or realtor account
def create_user():
    print("Are you a student or realtor?")
    print("1. Student \n2. Realtor\n")
    role = int(input("You are: "))
    if role == 1:
        is_student()
    elif role == 2:
        is_realtor()


# choice to lgin student or realtor
def login_user():
    print("Student or Realtor?")
    print("1. Student \n2. Realtor\n")
    login_type = int(input("You are: "))
    if login_type == 1:
        student_login()
    elif login_type == 2:
        realtor_login()


# when create account is student
def is_student():
    print("\nPlease complete the following: ")
    user = Student(input("Username: ").capitalize(),
                   input("Phone: ").capitalize())
    # call method from student class
    user.create_account(user.username, user.phone)


# when crete acount is realtor
def is_realtor():
    print("\nPlease complete information about yourself and you r rental: ")
    user = Realtor(input("Username: "),
                   input("Phone number: "),
                   input("City: "),
                   input("Suburb: "),
                   input("Rooms: "))
    # call method from realtor class
    user.create_account(user.username, user.phone_number, user.city, user.suburb, user.rooms)


def student_login():

    found = False
    print("\nPlease login")
    while not found:
        # imput of details to login
        name = input("Enter your name: ").capitalize()
        phone = input("Number: ").capitalize()
        # open student file
        with open(students_file, 'r') as all_students:
            student_data = csv.reader(all_students)

            for student in student_data:
                # check if input has matches in file
                if (name and phone) in student:
                    print("\nWelcome " + name.capitalize())
                    search_apartment()
                    found = True
                    break
                else:
                    print("User not found. Try again")
                    break
        all_students.close()


def realtor_login():
    pass


def search_apartment():
    print("What kind of a place would you like?: ")
    find = Realtor(input("What city?: ").capitalize(),
                   input("What suburb?: ").capitalize(),
                   input("How manny room?: ").capitalize())

    find.apartment_info(find.city, find.suburb, find.rooms)


main()
This is where my classes are:
import csv
WRITE = 'w'
APPEND = 'a'
READWRITE = 'w+'


# class for students
class Student:
    def __init__(self, username, phone):
        self.username = username
        self.phone = phone

    # method to create student account
    def create_account(self, username, phone):
        self.username = username
        self.phone = phone

        # create syudent file and append it
        students_file = "students.txt"

        file = open(students_file, mode=APPEND)
        file.write(username + "," + phone + "\n")
        file.close()
        print("\nThank You. Your Account has been created successfully")


# realtor class
class Realtor:
    def __init__(self, username, phone_number, city, suburb, rooms):
        self.username = username
        self.phone_number = phone_number
        self.city = city
        self.suburb = suburb
        self.rooms = rooms

    # method to create realtor account
    def create_account(self, username, phone_number, city, suburb, rooms):
        self.username = username
        self.phone_number = phone_number
        self.city = city
        self.suburb = suburb
        self.rooms = rooms

        # create realtor file and append to it
        realtor_file = "realtor.txt"

        file = open(realtor_file, mode=APPEND)
        file.write(username + "," + phone_number + "," +
                   city + "," + suburb + "," + rooms + "\n")

        print("\nThank You. Your Account has been created successfully")

    def apartment_info(self, city, suburb, rooms):
        self.city = city
        self.suburb = suburb
        self.rooms = rooms

        realtor_file = "realtor.txt"

        # open realtor file
        with open(realtor_file, 'r') as all_realtors:
            realtor_data = csv.read(all_realtors)
            # check if searched parameter exist in the file
            for realtor in realtor_data:
                if (city and suburb and rooms) in realtor:
                    print("Found")

        all_realtors.close()

    def apart_info(self):

        return '{} {} {}'.format(self.city, self.suburb, self.rooms)
And the error:
Traceback (most recent call last):
  File "C:/Users/Jacques/PycharmProjects/Mason_Housing/main.py", line 112, in <module>
    main()
  File "C:/Users/Jacques/PycharmProjects/Mason_Housing/main.py", line 11, in main
    option_select()
  File "C:/Users/Jacques/PycharmProjects/Mason_Housing/main.py", line 28, in option_select
    login_user()
  File "C:/Users/Jacques/PycharmProjects/Mason_Housing/main.py", line 48, in login_user
    student_login()
  File "C:/Users/Jacques/PycharmProjects/Mason_Housing/main.py", line 90, in student_login
    search_apartment()
  File "C:/Users/Jacques/PycharmProjects/Mason_Housing/main.py", line 107, in search_apartment
    input("How manny room?: ").capitalize())
TypeError: __init__() missing 2 required positional arguments: 'suburb' and 'rooms'
I have been trying to locate where I left an argument but I don't see where it is. Would you please show me what I am supposed to do

Thank you
Reply
#2
Look on line 29 of your classes file. You define five arguments to the __init__ for the Realtor class: username, phone_number, city, suburb, and rooms. Then on line 107 in main.py, you only have three input statements to generate those arguments. It appears you are not supplying username or phone_number, so you need to supply those from somewhere.

Using inputs when calling the class is kind of odd. I would gather the inputs separately, and then create the instance:

city = input("What city?: ").capitalize()
suburb = input("What suburb?: ").capitalize()
rooms = input("How manny rooms?: ").capitalize()
find = Realtor(username, phone_number, city, suburb, rooms)
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Oh I see, thank you. So it should be like this, yes?:
    print("What kind of a place would you like?: ")
    username = ""
    phone_number = ""
    city = input("What city?: ").capitalize()
    suburb = input("What suburb?: ").capitalize()
    rooms = input("How manny rooms?: ").capitalize()

    find = Realtor(username, phone_number, city, suburb, rooms)

    find.apartment_info(city, suburb, rooms)
Another question. If a realtor logged in, what would be the best way to return the data in their row? for example the row with Markus
James,0774755572,Harare,Avenues,2
Markus,0773012521,Bulawayo,Nkayi,1
Viktor,0772542779,Mutare,Wallcourt,3
Reply
#4
Looking at the rest of your program, it's completely disjoint. Every function is completely independent of every other function, without passing any parameters or returning any values. When you log the user in you create an object instance for them. That instance should be maintained in the program, and passed to the different functions for use in doing searches or displaying information about the account. There is a basic function tutorial, linked in my signature below, that can show you use parameters and return values.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
Thank you I will read through the tutorial and modify them.

Could that also be why if I try to login the second or third user it says they don't exist even though they are in the file?
Reply
#6
No, that's a problem with your for loop that searches the file:

            for student in student_data:
                # check if input has matches in file
                if (name and phone) in student:
                    print("\nWelcome " + name.capitalize())
                    search_apartment()
                    found = True
                    break
                else:
                    print("User not found. Try again")
                    break
First of all, the if statement is wrong. That's not how and words. You would need something more like if name in student and password and student:. That's a proper use of and, but it's not really a good way to check for a login. You seem to be storing the data with comma separated values. You need to separate out those values (like with split(',')), and check the correct values in the data against those input by the user.

Say you get that working. You check the first data item against what was input. Let's assume it doesn't match. So it goes to the else statement, which says the user was not found, and breaks out of the search loop. So you loop will only ever match the first user in the data. It will never login anyone else.

The fix involves unindenting the else statement (and removing the break):

            for student in student_data:
                # check if input has matches in file
                if (name and phone) in student:
                    print("\nWelcome " + name.capitalize())
                    search_apartment()
                    found = True
                    break
            else:
                print("User not found. Try again")
Now the else is on the for, not on the if. The else on a for loop is triggered if the loop ends normally, without a break statement. Since you break when you find someone, the else will trigger if you don't find anyone.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#7
Oh alright, thank you. Daeling with the functions now

Also, Instead of
if (name and phone) in student:
It would be better to use a nested if perhaps?
Reply
#8
No, it would be better to break out name and phone from student, and test those directly. You appear to be using comma separated values, so fields = student.split(',') will give you a list of the different items that are stored for that student. One of those is the name of the student, and one is their name. Test those. So maybe if name == fields[0] and phone == fields[1]: if I am reading your data example correctly.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  TypeError: missing 3 required positional arguments: wardancer84 9 10,822 Aug-19-2021, 04:27 PM
Last Post: deanhystad
  TypeError: max_value() missing 2 required positional arguments: 'alpha' and 'beta' Anldra12 2 4,198 May-15-2021, 04:15 PM
Last Post: Anldra12
  TypeError: add() missing 2 required positional arguments NectDz 5 13,044 May-28-2020, 02:54 PM
Last Post: BitPythoner
  Missing 2 Required Positional Arguments: SwiftWater 1 19,975 Feb-28-2019, 08:57 AM
Last Post: buran
  Functions (Arguments Passing,Changing a mutable ,Assignment to Arguments Names) Adelton 2 3,853 Mar-02-2017, 10:23 PM
Last Post: zivoni

Forum Jump:

User Panel Messages

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