Skip to content

Instantly share code, notes, and snippets.

@accessnash
accessnash / refactory.py
Created October 19, 2015 07:19
Python examples
small_words = ('into', 'the', 'a', 'of', 'at', 'in', 'for', 'on')
def book_title(title):
""" Takes a string and returns a title-case string.
All words EXCEPT for small words are made title case
unless the string starts with a preposition, in which
case the word is correctly capitalized.
>>> book_title('DIVE Into python')
'Dive into Python'
lst_UC = []
lst_LC = []
s = input("Enter your text: ")
words = s.strip().split()
for word in words:
if not word.islower():
lst_UC.append(word)
else:
"""Encode a string input"""
text = input("Enter a sentence: ")
code_char = ''
for letter in text:
code_char += chr(ord(letter) + 1)
lst = list(code_char)
secretcode = reversed(lst)
sec_code = ''
text = " We hold these truths to be self-evident, that all men are created equal, that they are endowed by their Creator with certain unalienable Rights, that among these are Life, Liberty and the pursuit of Happiness. - That to secure these rights, Governments are instituted among Men, deriving their just powers from the consent of the governed, - That whenever any Form of Government becomes destructive of these ends, it is the Right of the People to alter or to abolish it, and to institute new Government, laying its foundation on such principles and organizing its powers in such form, as to them shall seem most likely to effect their Safety and Happiness. Prudence, indeed, will dictate that Governments long established should not be changed for light and transient causes; and accordingly all experience hath shewn that mankind are more disposed to suffer, while evils are sufferable than to right themselves by abolishing the forms to which they are accustomed. But when a long train of abuses and usurpations,
"""Produce a multiplication table from a tuple of two-element tuples"""
data = ((1, 1), (2, 2), (12, 13), (4, 4), (99, 98))
for a, b in data:
print("{:>4} = {:>2} X {:>2}".format(a*b, a, b))
"""Create a list of words from a line of input"""
words = set()
words_dict = {}
while True:
text = input("Enter a sentence: ")
if not text:
print("Finished")
break
"""Create a file to display content"""
f = open('output_file.txt', 'r')
print(f.read())
while True:
text = input("Enter text: ")
if not text:
print("Finished")
break
"""Guessing game using random module"""
import random
secret = random.randint(1, 99)
guess = 0
while True:
guess = int(input("Guess a number: "))
if guess > secret:
print ("Too high")
secret = 17
guess = 0
guess_count = 1
while guess != secret:
guess = int(input("Guess a number: "))
if guess_count >= 5:
print("You've exceeded the maximum number of tries. The secret number was:", secret)
break
elif guess > secret:
print ("Guess lower. No of tries left:", 5- guess_count)
"""Counts the frequency for different numbered words in the text file"""
import sys
def read_text():
inputF = open(sys.argv[1], 'r')
return inputF
openF = read_text()
text = openF.read()