Skip to content

Instantly share code, notes, and snippets.

@zemadi
Created September 14, 2013 20:02
Show Gist options
  • Save zemadi/6565142 to your computer and use it in GitHub Desktop.
Save zemadi/6565142 to your computer and use it in GitHub Desktop.
These gists are from an intro to Python class I took in July 2013.
#This assignment was to play with text.
# 1) Ask the user for their name
# 2) Print their name backwards
# 3) Print their name 10 times with spaces between each name.
#name = raw_input ("What is your name?")
name = "Zhila"
print name[::-1]
#print name[1:]
#print name[0:3:2]
letters=[]
for char in name:
print letters.append(char)
print "Letters of my name:", letters
letters.reverse()
print letters
print "".join(letters)
for i in range(10):
print name,
#print (name+" ")*10
#This assignment was to create a number guessing game, using the parameters below.
# 1) Ask the user their name.
# 2) Generate a random number between 1 and 20.
# 3) Give the user 5 guesses to guess the number.
# 4) If the user is wrong, tell them "high" or "low"
# 5) If the user is right, tell them they won and exit.
# 6) If they don't get the answer right in 5 tries, tell them they lost and exit.
from random import randint
import shelve
stats = shelve.open("guess_stats.shelf")
name = raw_input("What is your name?")
name = str(name)
if name in stats:
print "Welcome back", name
user_stats = stats[name]
else:
print "Welcome to the game", name
user_stats = {"wins": 0, "losses":0}
print "You have %s wins and %s losses" % (user_stats["wins"], user_stats["losses"])
random_number = randint(1,20)
print(random_number)
guess = raw_input("Guess what the number is, between 1 and 20:")
try:
guess = int(guess)
except ValueError:
guess = int(raw_input("Not an integer. Try again."))
for i in range(5):
if guess > 20 or guess < 0:
guess = int(raw_input("Please give me a number between 1 and 20."))
if random_number > guess:
guess = int(raw_input("Too low! Try again."))
elif random_number < guess:
guess= int(raw_input("Too high! Try again."))
else:
print "You win!"
if guess == random_number:
print "You win!"
user_stats["wins"] += 1
else:
print "So sorry, you lost."
user_stats["losses"] += 1
stats[name] = user_stats
stats.close()
#This assignment was to pull text from a .txt copy of A Tale of Two Cities, count the words and distinct words, and then do search functions with the text.
# 1) Get all the stuff from the file.
# 2) Turn the text into words.
# 3) Count the total words
# 4) count the distinct words.
f = open("TaleOfTwoCities.txt", "r")
text = f.read()
f.close()
print "Hello world, I'm learning python".split()
words = text.split()
#print type(words)
print "Total number of words in ToTC:", len(words)
word_counts = {}
for word in words:
if word in word_counts:
word_counts[word] +=1
else:
word_counts[word] = 1
#for word, count in word_counts.items()[:10]:
# print word, count
#print word_counts.items()[:5]
ordered_counts = sorted(word_counts.items(),
key=lambda x: x[1],
reverse=True)
for word, count in ordered_counts[:10]:
print count, word
find_word = raw_input("What word are you looking for? ")
if find_word in word_counts:
print "Yes, %s is in the book." % find_word
else:
print "Nope, %s is not in book." % find_word
# Get all words that are less than or equal to 10
for word,count in word_counts.items():
if count <= 2:
#print word, "appears", count, "times"
print "%s appears %s times." % (word, count)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment