1
\$\begingroup\$

I wrote rock paper scissors game using what I have learnt yet.It is while,for statements,list,tuple,dictionary and such simple things only.So I am interested how can I simplify this code using only what I have learnt(simple things)

Here is code

    k = input("Hello guys what are you names: ")
    j = input("and yours? ")
    print(f"""ok {k} and {j} let the game begin!"
    P.s. Type exit to quit game""")
    list = ["rock","paper","scissors"]
    while True:

        a_player = input(f"{k} choose: rock, scissors, paper : ").lower()
        if a_player == "exit":
            break
        b_player = input(f"{j} choose: rock, scissors, paper: ").lower()
        if b_player == "exit":
            break
        for type in list:
            if a_player == type and b_player==type:
                print("  Draw guys, draw")
                break
        if b_player != list[1] and b_player!=list[0] and b_player!=list[2] or (a_player != list[1] and a_player != list[0] and a_player != list[2]) :
            print("Please type correctly")

        if a_player == list[1]:
            if b_player == list[0]:
                print(f" {k} wins: paper beats rock")
            elif b_player ==list[2] :
                print(f"{j} wins: scissors beat paper")

        elif a_player == list[2]:
            if b_player == list[0]:
                print(f"{j} wins: rock beats scissors")
            elif b_player == list[1]:
                print(f"{k} wins: scissors beat paper")

        elif a_player == list[0]:
            if b_player == list[2]:
                print(f"{k} wins: rock beats scissors")
            elif b_player==list[1]:
                print(f"{j} wins: paper beats rock")
\$\endgroup\$
1
\$\begingroup\$

I'm new to this community, but I'll give this my best shot:

Avoid single letter variable names. Names like k and j convey little about what values they represent, so replace them with something like:

a_player_name = input("Hello guys what are you names: ")
b_player_name = input("and yours? ")

This code:

a_player = input(f"{k} choose: rock, scissors, paper : ").lower()

creates the need to catch errors down the line and is repeated so I recommend replacing it with:

 def input_choices(string):
    while True:
        choice = input(string)
        if choice.lower() in list:
            return choice
        else:
            print('Please type correctly')

a_player = input_choices(f"{k} choose: rock, scissors, paper : ")

This avoids the need for:

    if b_player != list[1] and b_player!=list[0] and b_player!=list[2] or (a_player != list[1] and a_player != list[0] and a_player != list[2]) :
        print("Please type correctly")

Which I hope you agree is a vast improvement. Remember you can always format conditionals like this to improve readablity:

if (
    b_player != list[0] and b_player != list[1] and b_player != list[2]
    or a_player != list[1] and a_player != list[0] and a_player != list[2]
):
    print("Please type correctly")

Or even better use the in keyword

if (
    a_player not in list
    or b_player not in list
):
    print("Please type correctly")

As for the actual rock-paper-scissors logic:

    if a_player == list[1]:
        if b_player == list[0]:
            print(f" {k} wins: paper beats rock")
        elif b_player ==list[2] :
            print(f"{j} wins: scissors beat paper")

    elif a_player == list[2]:
        if b_player == list[0]:
            print(f"{j} wins: rock beats scissors")
        elif b_player == list[1]:
            print(f"{k} wins: scissors beat paper")

    elif a_player == list[0]:
        if b_player == list[2]:
            print(f"{k} wins: rock beats scissors")
        elif b_player==list[1]:
            print(f"{j} wins: paper beats rock")

I'm sure this can be improved with table-driven logic, but I'm sure there's no end of examples for you to skim through in the 'Related' tab.

Hope that helped :)

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Thanks a lot for your advises \$\endgroup\$ – user224424 May 17 at 11:53

Your Answer

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