Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

trying to make an rps game with python, have some problems

Hi, Im trying to make a simple rps game. However when i try to take an input, the while loop keep prompting me for no reason this is the current snapshot: The file called rps.py https://w.trhou.se/itxy3yr7gq

Steven Parker

5 Answers

Steven Parker
Steven Parker
243,160 Points

Try giving the whole community some time to respond before tagging anyone in particular. :wink:

But it's always good to have a final "else" at the end of a "if/elif" chain to show you when anything unexpected happens:

        else:
            print(f"oops, either your {user_choice} or the computer's {computer_choice} was not recognized! :P")

Try adding that onto your tests and see if it helps you to spot the problem yourself. Write again if you still need help.


Here's a way to check for all the wining combinations at once:

        elif user_choice+computer_choice in ["PR","RS","SP"]:
            print(f"{convert(user_choice)} beats {convert(computer_choice)} Good work! :]")
            count_wins += 1
            count_loses_comp += 1
        elif user_choice in "PRS":
            print(f"{convert(user_choice)} loses to {convert(computer_choice)} :[")
            count_loses += 1
            count_wins_comp += 1
        else:
            print(f"Oops, {user_choice} Isn't a valid input, Try again! :]")  

Also, those "continue"s weren't necessary, since that's the default action of a loop.

Steven Parker Hello steven how u doing?, thanks for the reply However I couldn't spot the problem, the code seem fine but it still doesn't working. this is the current snapshot:

https://w.trhou.se/kjkpeo6kjm

thanks in advance :}

Steven Parker
Steven Parker
243,160 Points

Hint: an upper-case letter won't match a lower-case letter in an equality comparison.

Hi steven, i manage to get a working game :], I added a score system, the convert() function, and getWinner() function can u review the code? The snapshot:

https://w.trhou.se/3loh6lpb85

However, I can't think of a better way to check the game logic without those if else statements, do u have an idea?

Steven Parker

Steven Parker
Steven Parker
243,160 Points

You don't need to tag me on a question I've already answered, notices are automatic. And I ammended my answer. :arrow_heading_up:

I thought about something like that This check is really good and I will embrace this kind of thought, I mean strings can be concatenated and because of that we add them togheter with the plus sign and then i check if after they concatenated their combo is in a list which contains all the WIN situations, its amazing :] then u checked if user_choice is in "RPS" and if it is we lost because it doesn't matter what the computer_choice value is because we already know when we win and when we draw.

However i added another check to the losing situation, I find it to be more easy to understand rather than to write like u did :]

  elif user_choice + computer_choice in ["RS","PR","SP"]:
            print("{} beats {} Good job! :D".format(convert(user_choice),convert(computer_choice)))
            count_wins += 1
            count_loses_comp += 1
        elif user_choice in "RPS" and user_choice + computer_choice not in ["RS","PR","SP"]:
            print("{} loses to {} :[".format(convert(user_choice),convert(computer_choice)))
            count_loses += 1
            count_wins_comp += 1

Good job on the game :]?

Steven Parker
Steven Parker
243,160 Points

Well, that kind of violates the "DRY" principle ("Don't Repeat Yourself"). Since the code that goes with the first test handles every win combination, any time the code below it is reached, it is guaranteed to not be one of those combinations. So testing the inverse is redundant.

However, I understand your interest in clarity, so this is where comments are handy:

        elif user_choice+computer_choice in ["PR","RS","SP"]:  # is it a winning combination?
#...
        elif user_choice in "PRS":  # now we know it's not a win, but is it VALID?

And yes, you're doing great! Keep up the good work.

hey steven! I found a bug in the program that started to happen when i used the way u suggested, if i just enter when i need to choose r/p/s i got the line: "None loses to x"

i try to add a check to check if user_choice is None or empty string, however it's not working current snapshot: https://w.trhou.se/qexvakfb18

old snapshot before u suggested: https://w.trhou.se/3loh6lpb85

Steven Parker

Steven Parker
Steven Parker
243,160 Points

I wasn't aware that "None" was considered to be part of a string by the membership operator! But it's easily fixed when you eliminate the redundant test:

        elif user_choice in "RPS" and len(user_choice) == 1: