Results 1 to 2 of 2

Thread: "Travis" guest list name checker (Ziyad Yehia Udemy course)

  1. #1
    Join Date
    May 2007
    Location
    West Indies
    Beans
    497
    Distro
    Ubuntu

    "Travis" guest list name checker (Ziyad Yehia Udemy course)

    I'm taking a Udemy course (not for credit, just for fun) but I would still like to treat this as a homework exercise so for experienced forum members, please do not solve the problem for me. All I am asking is for hints and tips.

    The task involves verifying the contents of a list using conditionals. For a full explanation of the purpose of this program and for instructions from the teacher, you can read them in the README on my GitHub repo (second paragraph from the bottom).

    I kinda got the program to run. It used to meet the requirements set out by the instructor. But I am now taking the project in a new direction by consolidating the operations into two separate functions (one called ‘main’ and the other called ‘check’). It is extending this script that I am encountering a Name Error and a Type Error.

    Here is my script so far:

    Code:
    #!/usr/bin/env python3
    # This third iteration splits the code into two different functions
    
    def main():
        """
        Initializes the two primary variables and returns them for future use
        """
        guest_list = ["Justin Trudeau", "Joseph Stalin", "Caesar", "Nero",]
        user_name = input("What is your name? Please spell it in full! >>  \n")
        return (guest_list, user_name)
    
    
    def check(placeholder):
        """
        Verifies the user name input and determines if in guest list
        """
        if user_name in guest_list:
            print(f"Welcome, {user_name}!")
        if user_name not in guest_list:
            print(f"Hi {user_name}, but unfortunately you are not in the guest list.")
            include_proposition = input("Would you like to join us anyways? Please enter a Yes or a No >> \n")
            if include_proposition == ("Yes", "Y", "y"):
                guest_list.append(user_name)
                print(f"Thanks, {user_name}! You've been added. Please enter.")
            if include_proposition == ("No", "N", "n"):
                print("OK! Have a good night then, my friend!")
            else:
                print("Sorry, I didn't get that. Please enter your selection again")
    
    
    if __name__ == "__main__":
        placeholder = main()
        check(placeholder)
    Notice at line 13 I pass the `placeholder` variable (which I initialize at the end of the script) below the `if name` conditional. Here is NameError the traceback:

    $ python script3.py
    What is your name? Please spell it in full! >>
    Matt
    Traceback (most recent call last):
    File "script3.py", line 33, in <module>
    check(placeholder)
    File "script3.py", line 17, in check
    if user_name in guest_list:
    NameError: name 'user_name' is not defined
    I figure the check() function requires `user_name` and `guest_list` so I tried replacing the `placeholder` as a parameter at line 13 with `guest_list, user_name` which returns a TypeError:

    $ python script3.py
    What is your name? Please spell it in full! >>
    Matt
    Traceback (most recent call last):
    File "script3.py", line 33, in <module>
    check(placeholder)
    TypeError: check() missing 1 required positional argument: 'user_name'
    Here are my two questions:
    1. How do I call both functions (`main()` and `check()`) properly so as to avoid these tracebacks?
    2. What do you call the Python convention with `if __name__ == "__main__":`? When I Google "if __name__ == "__main__"" there is a very detailed and thorough Stack Overflow question and answer which explains very clearly how, why and when to use `if __name__ == "__main__":` however it doesn’t actually say what this Python convention is called. So what do you call this?


    For my future reference, this Udemy instructor Ziyad Yehia calls this the "Travis" application. This is the fourth project under the "Python Datastructures" section. The course is titled "The Python Bible: Everything You Need to Program in Python"
    My rig:
    IBM Personal System/2 Model 30-286 - - Intel 80286 (16 bit) 10 Mhz - - 1MB DRAM - - Integrated VGA Display adapter
    1.44MB capacity Floppy Disk - - PS/2 keyboard (no mouse)

  2. #2
    Join Date
    Jan 2017
    Beans
    235

    Re: "Travis" guest list name checker (Ziyad Yehia Udemy course)

    Hi Drone4Four


    1. guest_list and user_name are local variables in your function main(). You are pass ing a tuple consisting of a list and a string to check(). You need to extract those variables in check() by using assignment statements. Try putting print(placeholder) as the first line in check().
    2. I do not know if there is a name for it. You've probably seen this page in the docs: https://docs.python.org/3/library/__...light=__name__

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •