Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Validate if my mini project game can be past through Test_Python.py?

So basically i am creating my version of rock ,paper, scissor game as a python project and i need help running it through by testing or passing it which i forgot how to becuase i took a few days break working on my project and forgot how to test it and this is my project:

import random
import math

def play():
    user = input("What's your choice? 'g' for gun, 'i' for ice, 'k' for knife\n")
    user = user.lower()

    computer = random.choice(['g', 'i', 'k'])

    if user == computer:
        return (0, user, computer)


    if is_win(user, computer):
        return (1, user, computer)

    return (-1, user, computer)

def is_win(player, opponent):

    if (player == 'g' and opponent == 'k') or (player == 'k' and opponent == 'i') or (player == 'i' and opponent == 'k'):
        return True
    return False

def play_best_of(n):

    player_wins = 0
    computer_wins = 0
    wins_necessary = math.ceil(n/2)
    while player_wins < wins_necessary and computer_wins < wins_necessary:
        result, user, computer = play()

        if result == 0:
            print('It is a tie. You and the Machine have both chosen {}. \n'.format(user))

        elif result == 1:
            player_wins += 1
            print('You chose {} and the Machine chose {}. Yippy You won! ;[\n'.format(user, computer))
        else:
            computer_wins += 1
            print('You chose {} and the Machine chose {}. Darn it You lost! :[\n'.format(user, computer))

    if player_wins > computer_wins:
        print('You have won the best of {} games Congrats! What a player :D'.format(n))
    else:
        print('Sadly, the Machine has won the best of {} games. Better luck next time!'.format(n))


if __name__ == '__main__':
    play_best_of(3)

(Note: This is all i came up with i just need help passing through it, how do i test it basically?)

i did tr but i keep getting pops up of a few problems here and there. i do have

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

from project import play, is_win, play_best_of

def play():

    
def is_win():



def play_best_of(n):




if __name__ == '__main__':
    play_best_of(3)

for my test prompt but i forgot how to assert i think? PLEASE HELP!

>Solution :

I believe you’re talking about writing unit tests. There are two libraries commonly used for unit testing in Python, the built in unittest library and the third-party pytest.

I would personally recommend that you use pytest because the syntax is much simpler. Refer to the unittest and pytest docs for further usage information.

A basic passing pytest example:
test_win.py

from project import is_win

def test_win_gk():
    assert is_win('g', 'k') == True

You can run this test by executing pytest in the same directory. This will execute every test in each file prefixed or suffixed with test. (test_something.py or something_test.py)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading