Anonymous user
Rock-paper-scissors: Difference between revisions
→{{header|Python}}: Less than half the length of the old program. New version more Pythonic, old version felt Java-focused.
m (→{{header|Sidef}}: code simplifications) |
(→{{header|Python}}: Less than half the length of the old program. New version more Pythonic, old version felt Java-focused.) |
||
Line 3,277:
=={{header|Python}}==
A light version with limited support for additional rules. New weapons can be introduced, but each can only be beaten by one other weapon.
The <code>rules</code> dictionary is of the form <code>'this': beaten by 'that', etc</code> as opposed to <code>'this': beats 'that'</code>.
from random import choice, randrange▼
WHATBEATS = { 'paper' : 'scissors',▼
'scissors' : 'rock',▼
while True:▼
human = input('\nchoose your weapon: ')
computer = rules[choice(previous)] # choose the weapon which beats a randomly chosen weapon from "previous"
print('the computer played', computer, end='; ')
if human in ('quit', 'exit'): break
return b▼
elif human in rules:
previous.append(human) # add human's choice to "previous"
if rules[computer] == human: # if what beats the computer's choice is the human's choice...
print('yay you win!')
elif rules[human] == computer: # if what beats the human's choice is the computer's choice...
print('the computer beat you... :(')
else: print("that's not a valid choice")</lang>
Output, where player always chooses Rock:▼
<pre>choose your weapon: rock
the computer played rock; it's a tie!
choose your weapon: rock
▲while True:
the computer played scissors; yay you win!
choose your weapon: rock
the computer played paper; the computer beat you... :(
choose your weapon: rock
the computer played paper; the computer beat you... :(
▲Output, where player always chooses Rock:
choose your weapon: rock
the computer played paper; the computer beat you... :(</pre>
=={{header|Rascal}}==
|