Reputation: 1
main.py:
import functions
def main():
count = True
while count == True:
print("Enter R for Rock")
print("Enter P for Paper")
print("Enter S for Scissors")
value = input("Choose from R/P/S: ") # taking input from the user, they can tpye R/P/S or r/p/s to play a game. Rock Paper Scissors
if value == "R" or value =="r":
print()
print(functions.rock(value))
elif value == "P" or value =="p":
print()
print(functions.paper(value))
elif value == "S" or value =="s":
print()
print(functions.scissors(value))
#Using If statement so if the user input is "R" then it will call the rock fucntion, if its "P" it will call the paper function and if its "S" it will call the scissors function.
else:
print()
print("Invalid Input, Please enter R for Rock, P for Paper, S for Scissors")
# if there's an invalid input the program will stop and redirect them to the correct input process
break
print("------------")
main()
function.py:
import random
def computer_choice():
choice = random.randint(1, 3)
if choice == 1:
return "Rock"
elif choice == 2:
return "Paper"
elif choice == 3:
return "Scissors"
# getting a random string from a,b,c variable. The random variable will be the computers input
def rock(value):
print("Computer choice:", computer_choice())
print()
if computer_choice() == "Rock":
return("Its a draw")
elif computer_choice() == "Paper":
return("The computer wins")
else:
return("You Win")
def paper(value):
print("Computer choice:", computer_choice())
print()
if computer_choice() == "Rock":
return("You Win")
elif computer_choice() == "Paper" :
return("Its a draw")
else:
return("The computer wins")
def scissors(value):
print("Computer choice:", computer_choice())
print()
if computer_choice() == "Rock":
return("The computer wins")
elif computer_choice() == "Paper":
return("You win")
else:
return("Its a draw")
# defining all the functions and giving each function a decision structure and the pattern for rock paper scissors.
Why is a random result for the code being displayed? The only thing random was supposed to be the choice of the computer but the results are random too The code seems to send random results from the functions where I used the random function. The "function.py" returns a random result and not the one that was supposed to be send
Upvotes: 0
Views: 37
Reputation: 3944
Every time you use computer_choice()
in an if statement it is generating a new random choice because you are calling the function. Instead you want to make a variable and assign computer_choice()
to it, then refer to this variable in your if statements.
For example rock
would become:
def rock(value):
comp = computer_choice()
print("Computer choice:", comp)
print()
if comp == "Rock":
return("Its a draw")
elif comp == "Paper":
return("The computer wins")
else:
return("You Win")
Upvotes: 1