Can Computers Think?
Introduction to Computer Science

CSC 106
Union College
Winter 2010

Homework exercise to prepare for class on Wednesday, 1/20


If you have any problems with any of these issues, don't hesitate to come see me.

Exercise: write a Python program that plays Rock-Paper-Scissors (over and over again)

This Wikipedia page explains how to play Rock-Paper-Scissors.

Step 1:

Write a function that plays rock-paper-scissors with the user. An interaction could, for instance, look like this:

>>> play()
Let's play Rock-Paper-Scissors!
Please type a number to make a choice:
0 -> rock
1 -> paper
2 -> scissors

What do you choose?    1 [Enter]
You chose:  1
I chose:  2
I win.

Step 2:

We did not get to while loops in class. So, you are not required to do this second step. But you are welcome to try it. While-loops in Python look the same as in rur-ple. Here is an example of a while loop that lets the user input numbers and prints them back to the screen until the user inputs 0.

print "Please input a number. I will echo it back to you and ask you for the next number. this will keep on going until you input a 0."

number = input("Your number please: ")

while number != 0:
    print "You typed " + str(number)
    number = input("Please input the next number: ")

print "You typed 0. Bye, bye."		

Now, write a while loop that calls your Rock-Paper-Scissor function and asks the player after each round whether he/she wants to play another round and keeps playing until the player answers 'no'.

Remember: use 'input', if you are asking the user to input numbers, and 'raw_input', if you are asking the user to input strings.

Optional Extensions

Note that the extensions do not depend on the second step. You can try them even if you did not do the second step with the while loop.

Extension 1:

Extend your program to sometimes cheat. However, to make it not too obvious, it should only have a 40% chance of cheating in each game.

Extension 2:

Write a rock-paper-scissors game with 5 options (instead of 3) like described at this site.

What to turn in

1) Submit your solution on Blackboard:

2) Bring a print-out of your Python code to class.