Python Forum
Critique first project
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Critique first project
#1
Can I get some feedback on the below code. I've just started very recently and would like to learn python.
How could I go about making the below code better? what could I do differently ?
what should I consider when asking about continuing the game, So only 'y' / 'n' are accepted. And Rock Paper, Scissors can only be selected

I to add, the main game into a function, but was having issues?


import random

choice = 0
rps = ["Rock", "Paper", "Scissors"]
cpu = random.choice(rps)  
start = 0
cont = "y"

print("lets play rock paper scissors")
print("Do you want to play Y/N")
start = input() # input the intention to play the game Y 


while start == cont: 
  print (rps)
  print ("Make your choice")
  print (cpu)
  choice = input()  # choice is made
  

  if choice == cpu :
       print("well done")
       print("Do you want to play again. Y/N")
       cont = input()
  elif choice != cpu:
      print("You lose")
      print("Do you want to play again. Y/N")
      cont = input()
      break
Reply
#2
Well, I don't like this style of input:

print('Do you want to play again. Y/N')
cont = input()
I'm never sure if it's a style choice, or if people don't realize you can pass the question to input:

cont = input('Do you want to play again (Y/N)? ')
The above code gets the exact same information.

Your while loop is kind of odd. You keep going as long as the answer to one question is the same as the answer to another question. That works, it's just odd when you think about it. It's good to avoid that when programming. "odd when you think about it" makes your code harder to maintain and debug.

So what is a standard conception of the process? I'd go with "we want to keep playing until they say they don't want to play any more." That concept as code is more like this:

while True:    # keep playing
    play_game()
    play_again = input('Do you want to play again (Y/N)? ')
    if play_again.lower() == 'n':            # until they don't want to play anymore.
        break
Note the use of the lower method. This allows the user to enter upper case or lower case and still be understood. I like to go a little further, and provide several possible answers:



no = ('n', 'no', '0', 'no way', 'nein', 'nyet')
while True:    # keep playing
    play_game()
    play_again = input('Do you want to play again? ')
    if play_again.lower() in no:            # until they don't want to play anymore.
        break
I think that allows for a better user experience.

As to the problem of the game not working, I usually do rock-paper-scissors with a dictionary of what beats what: wins = {'rock': 'scissors', 'paper': 'rock', 'scissors': 'paper'}. Then you have three possibilities to check for: if choice and cpu are the same, it's a draw; if wins[choice] is the same as cpu, the human wins; and otherwise (if wins[cpu] is the same as choice), the computer wins.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Thank you that makes a lot of sense.

About how the game works, I think i got caught up in getting the loop to work. I see I need to work on the game a bit still.

the rest of your suggestions are great, ill keep in mind.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020