1
\$\begingroup\$

The object of the code is to prompt the user with a quiz and collect the g's, y's, 0's, and 1's and if there is more than one then the user passes (else, they fail). I am a beginner and would like to know if there is a more efficient direction for this code to go in.

from time import sleep
print(pic1)


def check(name):
    if name.lower() == "brandon":
        print("Welcome home master")
    else:
        print("Let's find some data")
        sleep(0.5)
        print("What is your favorite color?")
        color = input("fav_color>>> ")
        if color == "":
            print("No skipping!")
            print(check(input("Enter Name Here>>> ")))
        print("What is your dream car?")
        car = input("fav_car>>> ")
        if car == "":
            print("No skipping!")
            print(check(input("Enter Name Here>>> ")))
        print("What team are you on?")
        team = input("team_name>>> ")
        if team == "":
            print("No skipping!")
            print(check(input("Enter Name Here>>> ")))
        print("Who is your best friend?")
        crush = input("best_friend>>> ")
        if crush == "":
            print("No skipping!")
            print(check(input("Enter Name Here>>> ")))
        print("What color is your shirt?")
        shirt = input("shirt_color>>> ")
        if shirt == "":
            print("No skipping!")
            print(check(input("Enter Name Here>>> ")))
        print("You got clout?")
        clout = input("clout_level>>> ")
        if clout == "":
            print("No skipping!")
            print(check(input("Enter Name Here>>> ")))
        print("Do you think you passed the test?")
        passed = input("passed?>>> ")
        if passed == "":
            print("No skipping!")
            print(check(input("Enter Name Here>>> ")))
        print("Calculating...")
        sleep(2.5)

        def add():
            color_counted = color.count("g")
            car_counted = car.count("g")
            team_counted = team.count("g")
            crush_counted = crush.count("g")
            shirt_counted = shirt.count("g")
            clout_counted = clout.count("y")
            clout_counted2 = clout.count("1")
            clout_counted3 = clout.count("0")
            passed_counted = passed.count("y")
            total = color_counted + car_counted + team_counted + clout_counted3 + passed_counted
            total = crush_counted + shirt_counted + clout_counted + clout_counted2
            return total
        if add() > 1:
            print("passed")
        else:
            print("failed")
        print("Congrats on your result!")
        sleep(100)


check(input("Enter Name Here>>> "))
\$\endgroup\$
  • \$\begingroup\$ also do not mind the print(pic1) at the beginning of the code, it was something I was messing around with and forgot to delete. \$\endgroup\$ – pythonier500 Sep 9 at 22:11
  • 2
    \$\begingroup\$ Welcome to Code Review! I made the title a bit more descriptive of the code. From How to Ask: "State what your code does in your title, not your main concerns about it.". If there is a more appropriate description then feel free to update it via edit. \$\endgroup\$ – Sᴀᴍ Onᴇᴌᴀ Sep 9 at 23:35
2
\$\begingroup\$

Welcome to CodeReview! This isn't great code, but that doesn't mean that this is a bad question. I think that you've come to the right place.

Recursion

    if color == "":
        print("No skipping!")
        print(check(input("Enter Name Here>>> ")))

This uses recursion when it shouldn't. In other words, check is calling itself needlessly, and if the user tries to "skip" enough times, you'll get a stack overflow. Instead of doing this, just loop until you have valid input.

Fake delays

    print("Calculating...")
    sleep(2.5)

Few things bother me more than when a user interface lies to me. This output suggests that it takes 2.5 seconds for a calculation to be done, but that simply isn't so. Don't lie to your users - just show the results.

sleep(100) is actively harmful - the user needs to Ctrl+C to kill the program; otherwise it sits there preventing the user from getting their shell terminal back. This should just be deleted.

Don't repeat yourself

There are many places in the program that are expressed in a repetitive manner, especially user input (the "no skipping" mechanism), the call to count, and the additions to get total. I suggest the following:

def input_without_skip(prompt: str) -> str:
    while True:
        result = input(f'{prompt}>>>')
        if result:
            return result
        print('No skipping!')


def is_master() -> bool:
    return input_without_skip('Enter Name Here') == 'Hercules'


def get_counts():
    for question, prompt, chars in (
        (     'What is your favorite color?',   'fav_color', 'g'),
        (          'What is your dream car?',     'fav_car', 'g'),
        (            'What team are you on?',   'team_name', 'g'),
        (         'Who is your best friend?', 'best_friend', 'g'),
        (        'What color is your shirt?', 'shirt_color', 'g'),
        (                   'You got clout?', 'clout_level', 'y10'),
        ('Do you think you passed the test?',     'passed?', 'y')
    ):
        print(question)
        answer = input_without_skip(prompt)
        yield sum(answer.count(c) for c in chars)


def main():
    if is_master():
        print('Welcome home, master.')
        return

    print("Let's find some data!")
    total = sum(get_counts())

    if total > 1:
        print("passed")
    else:
        print("failed")
    print("Congrats on your result!")


if __name__ == '__main__':
    main()
\$\endgroup\$
  • \$\begingroup\$ Thanks great content the only reason I put sleep 100 is because I am converting this program into an .exe and it stops running as soon as showing the result without giving the user time to read it. \$\endgroup\$ – pythonier500 Sep 10 at 17:10
  • \$\begingroup\$ The solution is not to sleep, it's too change the way that you execute the program. Either execute it from a command window, or change the properties of your shortcut. \$\endgroup\$ – Reinderien Sep 10 at 17:12
  • \$\begingroup\$ Alright, and also since I am just getting into Python what is with the -> str while defining a function? Sorry to bother, but I just have some questions of the code you suggested. \$\endgroup\$ – pythonier500 Sep 10 at 17:21
  • \$\begingroup\$ Those are PEP484 type hints. That one says that the function returns a string. \$\endgroup\$ – Reinderien Sep 10 at 17:22
  • \$\begingroup\$ Alright so it is not necessary but helpful? \$\endgroup\$ – pythonier500 Sep 10 at 17:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.