Hi, I am putting some simple Python challenges together for my high school class. I have this challenge for a simple rock, paper, scissors game. I want to run an automatic test (input/output - Match) but part of the program is the computer generates a random number between 1 and 3 for an input. How can I represent that in the input field? Or should I not be using Match?
The solution program is here. How do I test it?
print ("----ROCK, PAPER, SCISSORS----")
import random
#generates a random number between 1 and 3
computer=random.randint(1,3)
#asks the user to input their choice and stores it in a variable
user=int(input("Enter your choice:\n1=Rock\n2=Paper\n3=Scissors\n"))
#outputs the computer's move
print("The computer has chosen",computer)
#outputs a different string depending on the outcome of the game
if computer==user:
print ("Tie game")
elif computer==1 and user==3:
print ("Computer wins")
elif computer==2 and user==1:
print ("Computer wins")
elif computer==3 and user==2:
print ("Computer wins")
else:
print ("You Win!")
Thanks! My code works, including the input(), but I cannot make a Test that would test if a students code is correct - Iâm just wondering if I can or not?
So if my student created a solution just like mine in my original post - then I want to run a test to check that it works - but because the first input is a randomly generated number by the computer, I cannot predict what it will be in the test - so I was wondering if there is a way that I do not know about, to test the solutions using the test feature of the education teams replits. Up to this point I have only used the âmatchâ tests but there doesnât seem the scope within that to cope with this code. I have no idea how to use the regex test or even if that would be suitable or not?
Here is a screen shop of my options on the match test and what I would want it to be, but obvioulsy canât.
You could modify this program so the first input is an integer s, which you use to seed the random number generator. After âimport randomâ, do ârandom.seed(s)â. Then the correct output is completely determined by the inputs (the seed plus the regular user inputs).
If you solve this would you mind posting? I have this exact assignment for my python class but I have them live demo it for me in class instead. Pulling grades from tests for this assignment would be great
Yea - there are other ways I could do this to get the test to work, but I cannot ask my pupils to design their program, just to suit the test system - I want them to write their code in the way I teach them. If I canât autograde then it is fine, but I was just hoping there was a way to do it that I didn;t know about lol.
I remembered this convo when I was redoing some my assignments. We way overthought the solution to thisâŚ
I just start out the assignment with a seeding function. If the students just press enter itâs ârandomâ. In your Input/Output test, make sure you give your initial seed value first, then continue as you would normally.
import random
###### THIS IS FOR THE AUTOGRADER. DO NOT EDIT THIS CODE. ######
def autograde_seed_set(seed=None):
if seed is not None:
try:
seed = int(seed)
random.seed(seed)
except ValueError:
return
seed = input("Just press enter to begin...\n")
autograde_seed_set(seed)
################################# BEGIN YOUR CODE BELOW #################################
print( random.randint(1, 10) )
print( random.randint(1, 10) )
print( random.randint(1, 10) )
print( random.randint(1, 10) )
print( random.randint(1, 10) )
print( random.randint(1, 10) )