Giter Club home page Giter Club logo

challenge-4's Introduction

Challenge-4

CodeBeans 1.0 - Challenge 4

  • Importing Required Package.
import random

Code_maker

  • Defining the function named as Code_maker which will choose the code from the list of words.
  • Declaring a variable named as Code and assigning an empty list to it.
  • Initializing a for loop.
  • Appending the random choice to the variable named as Code and returning it.
def Code_Maker():
    Code = []
    for i in range(4):
        Code.append(random.choice(['R', 'G', 'B', 'Y', 'W', 'V', 'O', 'P']))
    return Code

Feedback_Function

  • Defining the function named as Feedback_Function which will give the feedback according to the user's guess.
  • Declaring a variable named as Feedback and assigning a list to it with two values.
  • Initializing a for loop.
  • Checking if the Code and the user's guess matches.
  • If the condition is True then incrementing +1 to the 1st index of the Feedback list.
  • If the condition is False then incrementing +1 to the 2nd index of the Feedback list.
def Feedback_Function(Code, user_guess):
    Feedback = [0, 0]
    for i in range(4):
        if Code[i] == user_guess[i]:
            Feedback[0] += 1

        elif Code[i] in user_guess:
            Feedback[1] += 1
    return Feedback

User_Guess_Grabber

  • Defining the function named as User_Guess_Grabber which will store the user's guesses.
  • Declaring a variable named as user_guess and assigning a user input to it.
  • Initializing a while loop which runs if the length of the user's guess is not equal to 4 or the guess is not an alphabetic letter.
  • If the statement is True then declaring the same variable containing a user input with a placeholder as "Invalid".
def User_Guess_Grabber(number):
    user_guess = input("")
    while len(user_guess) != 4 or not user_guess.isalpha():
        user_guess = input("Invalid:  ")
    return user_guess

Main

  • Defining the function named as main which is the main branch of this process.
  • Declaring a variable named as number and assigning an integer user input to it.
  • Declaring a variable named as number and assigning an integer user input to it.
  • This piece of code is used to print the answer to ensure that the program is working.
  • Initializing a for loop.
  • Declaring a variable named as user_guess and calling the User_Guess_Grabber function with number as the parameter.
  • Printing the user's guess.
  • Declaring a variable named as Feedback and calling the Feedback_Function function with Code and user_guess as the parameters.
  • Printing the Feedback.
  • Checking if the Feedback is equal to 4.
  • If the condition is True then print You win and quit.
  • Checking if the Feedback is not equal to 4.
  • If the condition is True then print You lose! and the answer.
def main():
    number = int(input(""))
    Code = Code_Maker()
    # print(Code)
    for i in range(number):
        user_guess = User_Guess_Grabber(number)
        print("g:", user_guess)
        Feedback = Feedback_Function(Code, user_guess)
        print("f:", Feedback)
        if Feedback == [4, 0]:
            print("You win!")
            break
    if Feedback != [4, 0]:
        print("You lose!")
        print("The Answer:",Code)
  • Calling the main Function.
main()

challenge-4's People

Contributors

dawnsaju avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.