Skip to content

Instantly share code, notes, and snippets.

@subnomo
Created June 11, 2014 15:52
Show Gist options
  • Save subnomo/6efb0a9ca3009224ca57 to your computer and use it in GitHub Desktop.
Save subnomo/6efb0a9ca3009224ca57 to your computer and use it in GitHub Desktop.
A bunch of games written in Python.
def rock():
from random import randrange
import sys
'''Chooses random number and then assigns either rock, paper,
or scissors to variable "choice" depending on number. Returns choice.'''
def rps():
num = randrange(1, 4, 1)
if num == 1:
choice = 'Rock'
elif num == 2:
choice = 'Paper'
elif num == 3:
choice = 'Scissors'
return choice
rounds = int(input ("How many points are required to win? "))
i = 0
win = 0
lose = 0
while i < rounds:
choice = rps()
print ("Score: You", win, " Computer", lose)
humanchoice = input ("Rock, paper, scissors: ").lower()
if humanchoice.lower() == 'r': # Player can also enter either r, p, or s.
humanchoice = 'rock'
elif humanchoice.lower() == 'p':
humanchoice = 'paper'
elif humanchoice.lower() == 's':
humanchoice = 'scissors'
elif humanchoice.lower() == 'x':
sys.exit()
while humanchoice == choice.lower():
print ("Computer chooses", choice + ". Go again!")
choice = rps()
print ("Score: You", win, " Computer", lose)
humanchoice = input ("Rock, paper, scissors: ").lower()
if humanchoice.lower() == 'r': # Player can also enter either r, p, or s.
humanchoice = 'rock'
elif humanchoice.lower() == 'p':
humanchoice = 'paper'
elif humanchoice.lower() == 's':
humanchoice = 'scissors'
if humanchoice != choice.lower():
if choice == 'Rock':
if humanchoice == 'paper':
print ("Computer chooses", choice + ". You win this round.\n")
win += 1
else:
print ("Computer chooses", choice + ". You lose this round.\n")
lose += 1
elif choice == 'Paper':
if humanchoice == 'scissors':
print ("Computer chooses", choice + ". You win this round.\n")
win += 1
else:
print ("Computer chooses", choice + ". You lose this round.\n")
lose += 1
elif choice == 'Scissors':
if humanchoice == 'rock':
print ("Computer chooses", choice + ". You win this round.\n")
win += 1
else:
print ("Computer chooses", choice + ". You lose this round.\n")
lose += 1
i += 1
if (win / rounds) > 0.5:
print ("Score: You", win, " Computer", lose)
print ("You win!")
ask()
elif (win / rounds) == 0.5:
print ("Score: You", win, " Computer", lose)
print ("It's a tie!")
ask()
else:
print ("You lose.")
ask()
def piglatin():
# Takes a word as input and converts it into piglatin while also accounting for punctuation and capitalization.
def wordconvert(word):
vowels = ['a', 'e', 'i', 'o', 'u', 'y']
punc = list('`~!@#$%^&*()-_+=[]{}|:;,.<>/?\\"\'')
numbers = list('1234567890')
prefix = []
suffix = []
startpunc = ''
endpunc = ''
for index, letter in enumerate(word):
if letter not in vowels and letter not in punc:
prefix.append(letter)
elif letter in vowels:
break
for index, letter in enumerate(word):
if letter in punc:
if index == 0:
startpunc = letter
if letter == word[-1]:
endpunc = letter
if letter not in vowels and index <= len(prefix):
continue
elif index >= len(prefix):
if letter not in punc:
if letter != word[0] and letter != [-1]:
suffix.append(letter)
if word[0].lower() not in vowels and prefix[0] != prefix[0].lower():
suffix[0] = suffix[0].upper()
prefix[0] = prefix[0].lower()
if len(word) == 1:
return word + "yay"
elif word[0] in numbers:
return word
elif word[0] in vowels:
return word + "ay"
else:
return startpunc + ''.join(suffix) + ''.join(prefix) + "ay" + endpunc
# Splits a sentence input into a list and converts them into piglatin by using the function above.
# It then returns the translated sentence.
def sentconvert(sentence):
converted = []
sentence = sentence.split()
for word in sentence:
converted.append(wordconvert(word))
return ' '.join(converted)
while True:
english = input ("Translate to piglatin: ")
if english.lower() == 'q':
break
else:
print(sentconvert(english))
def powerball():
import random
# Loop makes a list of numbers, sorts it, adds a powerball number. Bam, done.
def generate():
numbers = []
i = 0
while i < 5:
numbers.append(random.randint(1, 53))
i += 1
numbers.sort()
powerball = random.randint(1, 42)
print("Your numbers:", ' '.join(str(num) for num in numbers), " Powerball:", powerball)
while True:
inp = input("\nHow many sets of numbers? ")
if inp.lower() == "q":
break
inp = int(inp)
x = 0
while x < inp:
generate()
x += 1
def ask():
print ("\n1. Rock, Paper, Scissors\n2. Piglatin\n3. Powerball")
inp = input("What do you want to play? ")
if inp == "1":
rock()
elif inp == "2":
piglatin()
elif inp == "3":
powerball()
elif inp.lower() == "q":
exit
ask()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment