-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestOne.py
145 lines (118 loc) · 6.13 KB
/
TestOne.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
from random import randrange
class Country: #creates a class for country
def __init__(self, name, population):
self.name = name
self.population = population
def display(self):
print("{0:<20}Population:{1:,}".format(self.name, self.population)) #prints the display
def getPopulation(self):
return self.population #grabs population
def getName(self): #grabs the name of the country
return self.name
def makeACountry(line):
item = line.split("\t")
name = item[0].strip() #strips everything after the first index which is a country name
pop = int(item[1].strip().replace(",","")) #strips, replaces the commas and turns the string of population number into an int
c = Country(name, pop) #uses the Country class while grabbing the country's name and its population
return c
def usePopulation(aCountry):
return aCountry.getPopulation()
def useName(aCountry):
return aCountry.getName()
def orderCountry():
countryList = []
infile = open("countries.txt","r") #opens file
for line in infile: #reads each line in the txt file and displays each line in the shell
country = makeACountry(line)
country.display() #displays the input from the txt file
countryList.append(country)
print("*"*50) #splits the original txt file output from the sorted list
question = input("Would you like to sort the list by name or by population? (Enter 'n' for name or 'p' for population.) ")
while not(question == "n" or question == "N" or question == "P" or question == "p"):
question = input("Would you like to sort the list by name or by population? (Enter 'n' for name or 'p' for population.) ")
#depending on user choice, it will sort wither by name or population and if anything else is entered it will, ask again
if ((question == 'n') or (question == 'N')):
countryList.sort(key=useName)
for i in countryList: #displays the countries alphabetically
i.display()
elif ((question == 'p') or (question == 'P')):
countryList.sort(key=usePopulation)
for i in countryList: #displays the countries from lowest number to highest number
i.display()
infile.close() #closes the file
def steps():
ask = input("Do you want to see all the steps for the shuffled deck?(y/n) ")
while not(ask == "y" or ask == "n" or ask == "Y" or ask == "N"): #asks the user for y or n input, if any other character is entered then it will keep asking
ask = input("Do you want to see all the steps for the shuffled deck?(y/n) ")
return ask
return ask
def makeDeck():
deck = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A'] #deck of cards
return deck
def Shuffle(deck, show): #shuffles the deck
num = len(deck) #takes length of the deck
for i in range(num): #reads and swaps the random index of the list of cards from the original index from the cards
random = randrange(i, num)
deck[i], deck[random] = deck[random], deck[i]
if show == True: #this will show each step with shuffling the deck
print(deck)
return deck
def continue_shuffle():
q = input("Would you like to shuffle again? (y/n) ") #asks to continue shuffling the deck
if (q == "Y" or q == "y"):
print("Let's start from beginning.")
cardShuffler() #will call the function again thus asking if user would like to shuffle again
elif (q == "N" or q == "n"):
print("Okay, shuffling program is done\n")
else:
continue_shuffle() #if anything else is entered other than n,y,N,Y , then it will cll the function again
def cardShuffler():
deck = makeDeck() #sets variable deck and calls makeDeck() which returns a list of a deck
print("Starting deck: ", deck)
ask = steps()
if (ask == "y" or ask == "Y"):
shuffle = Shuffle(deck,True)
else:
shuffle = Shuffle(deck,False)
print("Shuffled deck: " , shuffle)
continue_shuffle()
def roll(): #randomizes the number from 1 to 6
r = randrange(1,7)
return r
def diceCounter(): #creates a dice counter where it is the sum of two dice and how counts the frequecy of that number
counter = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
numRolls = int(input("How many times do you want to roll the two dice together? ")) #asks how many times you want to roll the two dice
for i in range(numRolls): #takes the number of rolls and loops it from 0 to the number of rolls
red = roll() #dice 1
green = roll() #dice 2
r = red + green #the 2 dice together
counter[r] = counter[r] + 1
count = 0
print("Sum of dice \tFrequency")
for value in counter:
print(count,"\t\t", value)
count = count + 1
def dictionary(): #creates a dictionary called deserts, prints the name from each index of the list and along with the length of the name
desserts = ['pie', 'candy', 'cake', 'chocolate', 'cookie']
for name in desserts:
print(name ,":" , len(name))
greetings = ['hi', 'hello', 'good morning', 'good afternoon', 'salutations', 'hey']
print( {word : len(word) for word in greetings} ) #concatenates the word in each index of greetings with the length of each word in the respected index
def try_except(): #it will ask the user for two numbers and it will try to divide the two numbers by each other and print the solution, and if not, it will print the zerodivision error.
a = int(input("Enter a number: "))
b = int(input("Enter another number: "))
try:
solution = a / b
except ZeroDivisionError:
print("Division by zero!")
else:
print("The answer is ", solution)
finally:
print("Goodbye!") #prints this to make sure that it is the end of the function
def main(): #calls the functions and the program will only run in what is in main
cardShuffler()
diceCounter()
dictionary()
try_except()
orderCountry()
main()