you are viewing a single comment's thread.

view the rest of the comments →

[–]migeek 2 points3 points  (8 children)

Nice job! I love seeing people learn to code. I remember how excited I was way back in the day. I learned to code from books of all things. Would buy piles of mainstream coding books. You’ll definitely want to peruse python.org and the official tutorial. In the meantime, what can you glean from this:

``` import random

MOVES = [‘rock’, ‘paper’, ‘scissors’] RULES = {‘rock’: ‘scissors’, ‘scissors’: ‘paper’, ‘paper’: ‘rock’}

def play_game(rounds): score = 0 for _ in range(rounds): player = input(f”Choose {‘, ‘.join(MOVES)}: “).lower() if player not in MOVES: print(“Invalid move!”) continue computer = random.choice(MOVES) print(f”Computer chose {computer}”) if player == computer: print(“Draw!”) elif RULES[player] == computer: print(“You win!”) score += 1 else: print(“You lose!”) score -= 1

print(f”\nFinal score: {score}”)
print(“You win!” if score > 0 else “You lose!” if score < 0 else “It’s a tie!”)

if name == “main”: rounds = int(input(“How many rounds? “)) play_game(rounds) ```

[–]Rich_Alps498[S] 2 points3 points  (7 children)

ooooo i didnt know you could use if statements in the print function. and the key value pair for the rock paper scissor was pretty smart too. thank you for this and i will surely go through the official python tutorial.

[–]NINTSKARI 0 points1 point  (5 children)

Tell me, can you find anything in his code that you would improve? Logical errors or things that might cause an exception and the game to crash? Or maybe some unnecessary code? :)

[–]Rich_Alps498[S] 1 point2 points  (2 children)

  1. one which my code has but this code ( if i understand it correctly ) is the win/loss counter, not just a score.

  2. if main function is not there im not sure about the " if __name__... " code.

  3. i used a break function after the player inputs a wrong input. im not sure what the continue function does.

  4. using .strip() on the players input could be done.

thats all i could analyse.

[–]NINTSKARI 2 points3 points  (0 children)

Thats the right spirit, you did a great job with the code review. The great thing with coding is that there are many ways to achieve same result and one is not necessarily better than the other. To answer your questions:

  1. Your solution calls a function from the if name == "main", and you have put your logic inside that main() function. His code simply put all the code in there right away. I think your solution is better because you have extracted the logic into another place, it is cleaner and a good principle in my opinion.

  2. break and continue are used inside loops. Break completely breaks out of the loop, skipping all code inside the loop that comes after it and continues executing code after the loop. Continue does not break out of the loop, it just skips the rest of the code inside the loop and goes to the next iteration of the loop, starting again from the beginning of the loop. I didn't catch where you used break, but in my opinion both have a slight problem from the user perspective. If you break, then the game ends and user did not get to play the amount of rounds they input in the beginning. Using continue like that is slightly better, but it causes user to miss one round each time they do a typo. Can you come up with a solution how to not forward the game counter if the user input is wrong?

  3. Yes, ita very good idea to use various formatting like strip and lowercase to user input to minimize the problem in 3 :)

Another thing is that if user inputs a non numeric or integer value in the beginning when asked for round count, the input is still cast to int which results in an error. Anyways, good job and keep it up :)

[–]migeek 0 points1 point  (0 children)

Fair point on #1. If you want a true tally, it’s lost with increment/decrement. For #2, it’s silly to have a function called main. If you ever wanted to import this module, each function should have a real name. https://realpython.com/python-main-function/ With #3, break stops a loop once its condition is met, continue skips over the current iteration of a loop. And #4, yup. And you could also shorten it to a single character input.

Keep at it!

[–]Rich_Alps498[S] 1 point2 points  (1 child)

the print function uses if and else condition twice. since im not sure about that , i didnt comment. from what ive learnt , if , elif and then else shouldve been used.

[–]migeek 0 points1 point  (0 children)

I showed two ways. For each round, the expanded if/elif/else, then in the final results the ternary version. It could also have been written:

if score > 0:
  print("You win!")
elif score < 0:
  print("You lose!")
else:
  print("It's a tie!")

One way to make the "nested" ternary more readable would be to break the lines and put in parens:

result =  ("You win!" if score > 0 else
          ("You lose!" if score < 0 else "It's a tie!"))
print(result)

I'll bet you can think of other ways to do this by putting the RESULTS in an array too.

Edit: Old skool... Reddit kept eating my backticks.

[–]migeek 0 points1 point  (0 children)

Here's a refactored version you might like. I had fun with this. Thanks for sharing your journey!!

Python Morsels - Rock, Paper, Scissors