#๐ Game Selection
22 messages ยท Page 1 of 1 (latest)
@drowsy sphinx
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
when i run this script it just says rock paper scissors and auto selects that
when u import a file u "run" it
not really
the work around is shoving watever starts the game into a function like def start()
then call that imported function in ur main file
whats inside that file u importing
my rock paper scissors game
just the code to run that game
import random
answer = ""
player_wins = 0
computer_wins = 0
draws = 0
while answer != "quit" and answer != "no":
Game_List = ["rock", "paper", "scissors"]
computer_choice = random.choice(Game_List)
action = input("Rock Paper Scissors. \n").lower()
print("You chose " + action + "\nI choose " + computer_choice)
if action == computer_choice:
print("Its a Draw!")
print(f'we both chose {action}')
draws += 1
elif action == "rock":
if computer_choice == "paper":
print("Computer Wins!")
computer_wins += 1
elif computer_choice == "scissors":
print("You Win!")
player_wins += 1
elif action == "paper":
if computer_choice == "scissors":
print("Computer Wins!")
computer_wins += 1
elif computer_choice == "rock":
print("You Win!")
player_wins += 1
elif action == "scissors":
if computer_choice == "rock":
print("Computer Wins!")
computer_wins += 1
elif computer_choice == "paper":
print("You Win!")
player_wins += 1
else:
print("Error, Invalid Input: scores not altered")
print("Player score = " + str(player_wins) + ", Computer score = " + str(computer_wins) + ", draws = " + str(draws))
answer = input("would you like to play another game (Yes/No/Quit)? \n").lower()
this is what u need to shove into a function
so just write that code inside a function
better still, create a class, initialise the variables as self variables in the init constructor, then have at the bottom of your python script ```py
if name == 'main':
c = NameOfClass()
output = c.call_whatever_function_you_want(insert vars here)
print(f"{output}")
@drowsy sphinx
i ended up just writing all the code into a function
Fair. This is what I was kind of referring to:
import random
class RockPaperScissors:
def __init__(self):
self.answer = ""
self.player_wins = 0
self.computer_wins = 0
self.draws = 0
self.Game_List = ["rock", "paper", "scissors"]
def play_game(self):
while self.answer != "quit" and self.answer != "no":
computer_choice = random.choice(self.Game_List)
action = input("Rock Paper Scissors. \n").lower()
print("You chose " + action + "\nI choose " + computer_choice)
if action == computer_choice:
print("Its a Draw!")
print(f'we both chose {action}')
self.draws += 1
elif action == "rock":
if computer_choice == "paper":
print("Computer Wins!")
self.computer_wins += 1
elif computer_choice == "scissors":
print("You Win!")
self.player_wins += 1
elif action == "paper":
if computer_choice == "scissors":
print("Computer Wins!")
self.computer_wins += 1
elif computer_choice == "rock":
print("You Win!")
self.player_wins += 1
elif action == "scissors":
if computer_choice == "rock":
print("Computer Wins!")
self.computer_wins += 1
elif computer_choice == "paper":
print("You Win!")
self.player_wins += 1
else:
print("Error, Invalid Input: scores not altered")
print("Player score = " + str(self.player_wins) + ", Computer score = " + str(self.computer_wins) + ", draws = " + str(self.draws))
self.answer = input("would you like to play another game (Yes/No/Quit)? \n").lower()
if __name__ == "__main__":
game = RockPaperScissors()
game.play_game()```
This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.