
DATA:
# happy2.py
import csv
def main():
happy_dict = make_happy_dict()
print_sorted_dictionary(happy_dict)
def make_happy_dict():
filename = "happiness.csv"
happy_dict={}
with open(filename, 'r') as infile:
csv_happy = csv.reader(infile)
infile.readline()
for line in csv_happy:
happy_dict[line[0]] = line[2]
return happy_dict
def lookup_happiness_by_country(happy_dict):
return
def print_sorted_dictionary(D):
if type(D) != type({}):
print("Dictionary not found")
return
print("Contents of dictionary sorted by key.")
print("Key","Value")
for key in sorted(D.keys()):
print(key, D[key])
main()
"happines.csv"
Country,Year of Estimate,Happiness Index
Afghanistan,2018,2.694303274
Albania,2018,5.004402637
Algeria,2018,5.043086052
Angola,2014,3.794837952
Argentina,2018,5.792796612
Armenia,2018,5.062448502
Australia,2018,7.17699337
Austria,2018,7.396001816
Azerbaijan,2018,5.167995453
Bahrain,2017,6.227320671
Bangladesh,2018,4.499217033
Belarus,2018,5.233769894
Belgium,2018,6.89217186
Belize,2014,5.955646515
Benin,2018,5.81982708
Bhutan,2015,5.082128525
Bolivia,2018,5.915734291
Bosnia and Herzegovina,2018,5.887401104
Botswana,2018,3.4613657
Brazil,2018,6.190921783
Bulgaria,2018,5.098813534
Burkina Faso,2018,4.92723608
Burundi,2018,3.775283098
Cambodia,2018,5.121837616
Cameroon,2018,5.250737667
Canada,2018,7.175496578
Central African Republic,2017,3.475862026
Chad,2018,4.486325264
Chile,2018,6.436220646
China,2018,5.131433964
Colombia,2018,5.983512402
Comoros,2018,3.972819567
Congo,2018,5.490214348
Costa Rica,2018,7.141074657
Cote d'Ivoire,2018,5.26837492
Croatia,2018,5.536271095
Cuba,2006,5.417868614
Cyprus,2018,6.276443005
Czech Republic,2018,7.034165382
Democratic Republic of Congo,2017,4.311033249
Denmark,2018,7.648785591
Djibouti,2011,4.369193554
Dominican Republic,2018,5.433215618
Ecuador,2018,6.128010273
Egypt,2018,4.005450726
El Salvador,2018,6.276246071
Estonia,2018,6.091302395
Ethiopia,2018,4.379262447
Finland,2018,7.85810709
France,2018,6.665903568
Gabon,2018,4.783009052
Gambia,2018,4.922099113
Georgia,2018,4.659097195
Germany,2018,7.118364334
Ghana,2017,5.481310844
Greece,2018,5.40928936
Guatemala,2018,6.626591682
Guinea,2018,5.25222683
Guyana,2007,5.992826462
Haiti,2018,3.614928007
Honduras,2018,5.908423901
Hong Kong,2017,5.362474918
Hungary,2017,6.065038681
Iceland,2017,7.476213932
India,2018,3.818068743
Indonesia,2018,5.340295792
Iran,2018,4.278117657
Iraq,2017,4.462399006
Ireland,2018,6.962335587
Israel,2018,6.92717886
Italy,2018,6.516526699
Jamaica,2017,5.889759064
Japan,2018,5.793575287
Jordan,2018,4.638933659
Kazakhstan,2018,6.00763607
Kenya,2018,4.655702591
Kosovo,2018,6.391825676
Kuwait,2017,6.093905449
Kyrgyzstan,2018,5.297383308
Laos,2018,4.85940218
Latvia,2018,5.901154041
Lebanon,2018,5.167186737
Lesotho,2017,3.795300722
Liberia,2018,4.134852886
Libya,2018,5.493977547
Lithuania,2018,6.308878899
Luxembourg,2018,7.242630959
Macedonia,2018,5.239834785
Madagascar,2018,4.070586681
Malawi,2018,3.334633589
Malaysia,2018,5.338817596
Mali,2018,4.415729523
Malta,2018,6.909710884
Mauritania,2018,4.313615322
Mauritius,2018,5.88174057
Mexico,2018,6.549578667
Moldova,2018,5.682277203
Mongolia,2018,5.464622974
Montenegro,2018,5.650189877
Morocco,2018,4.896791935
Mozambique,2018,4.653713703
Myanmar,2018,4.410633087
Namibia,2018,4.834087849
Nepal,2018,4.910086632
Netherlands,2018,7.463097095
New Zealand,2018,7.370285988
Nicaragua,2018,5.81895256
Niger,2018,5.164007187
Nigeria,2018,5.252288342
Northern Cyprus,2018,5.608056068
Norway,2018,7.444262028
Oman,2011,6.852982044
Pakistan,2018,5.471553802
Palestine,2018,4.5539217
Panama,2018,6.281434059
Paraguay,2017,5.71329546
Peru,2018,5.679661274
Philippines,2018,5.869172573
Poland,2017,6.201268196
Portugal,2018,5.919822693
Qatar,2015,6.374529362
Romania,2018,6.150878906
Russia,2018,5.513500214
Rwanda,2018,3.5610466
Saudi Arabia,2018,6.356393337
Senegal,2018,4.769377232
Serbia,2018,5.936493397
Sierra Leone,2018,4.305683136
Singapore,2018,6.374564171
Slovakia,2018,6.23511076
Slovenia,2018,6.249419212
Somalia,2016,4.667941093
South Africa,2018,4.8839221
South Korea,2018,5.840231419
South Sudan,2017,2.816622496
Spain,2018,6.513370991
Sri Lanka,2018,4.400223255
Sudan,2014,4.138672829
Suriname,2012,6.269286633
Swaziland,2018,4.211565018
Sweden,2018,7.374792099
Switzerland,2018,7.508586884
Syria,2015,3.46191287
Taiwan,2018,6.467004776
Tajikistan,2017,5.829234123
Tanzania,2018,3.445023298
Thailand,2018,6.011561871
Togo,2018,4.022894859
Trinidad and Tobago,2017,6.191859722
Tunisia,2018,4.741132259
Turkey,2018,5.185689449
Turkmenistan,2018,4.620601654
Uganda,2018,4.321714878
Ukraine,2018,4.661909103
United Arab Emirates,2018,6.603743553
United Kingdom,2018,7.233445168
United States,2018,6.882684708
Uruguay,2018,6.371714592
Uzbekistan,2018,6.205460072
Venezuela,2018,5.005663395
Vietnam,2018,5.295547009
Yemen,2018,3.057513952
Zambia,2018,4.041488171
Zimbabwe,2018,3.616479874
I need help with this program, also this program needs to be done in Python. Thank you
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks
Note: Please maintain proper code spacing (indentation), just copy the code part and paste it in your compiler/IDE directly, no modifications required.
#code
import csv
def main():
happy_dict = make_happy_dict()
# print_sorted_dictionary(happy_dict)
lookup_happiness_by_country(happy_dict)
def make_happy_dict():
filename = "happiness.csv"
happy_dict = {}
with open(filename, 'r') as infile:
csv_happy = csv.reader(infile)
infile.readline()
for line in csv_happy:
happy_dict[line[0]] = line[2]
return happy_dict
def lookup_happiness_by_country(happy_dict):
# looping
while True:
# reading a country name from user
name = input("Enter a country to lookup or 'done' to exit:")
# if input is 'done', returning from the method
if name == 'done':
return
# otherwise if name is in happy_dict
if name in happy_dict:
# printing happiness value associated with key=name
print(happy_dict[name])
else:
# otherwise printing not found
print(name, 'not found')
def print_sorted_dictionary(D):
if type(D) != type({}):
print("Dictionary not found")
return
print("Contents of dictionary sorted by key.")
print("Key", "Value")
for key in sorted(D.keys()):
print(key, D[key])
main()
#output
Enter a country to lookup or 'done' to exit:Mexico
6.549578667
Enter a country to lookup or 'done' to exit:Atlantis
Atlantis not found
Enter a country to lookup or 'done' to exit:Taiwan
6.467004776
Enter a country to lookup or 'done' to exit:Brazil
6.190921783
Enter a country to lookup or 'done' to exit:done
DATA: # happy2.py import csv def main(): happy_dict = make_happy_dict() print_sorted_dictionary(happy_dict) def make_happy_dict(): filename = "happiness.csv"...
PYTHON PROGRAM
by using functions & dictionary or set.
- must create a variable to hold a dictionary or sets
- must define a function that accepts dictionaries or sets as an
argument
A dictionary maps a set of objects (keys) to another set of objects (values). A Python dictionary is a mapping of unique keys to values. For e.g.: a dictionary defined as: released = { "iphone" : 2007, "iphone 3G": 2008, "iphone 3GS": 2009, "iphone 4" : 2010,...
This is a python question. Start with this program, import json from urllib import request def main(): to_continue = 'Yes' while to_continue == 'yes' or to_continue == 'Yes': currency_code = input("Enter currency code: ").upper() dollars = int(input("Enter number of dollar you want to convert: ")) # calling the exchange_rates function data = get_exchange_rates() if currency_code in data["rates"].keys(): currency = data["rates"][currency_code] # printing the the currency value by multiplying the dollars amount. print("The value is:", str(currency * dollars)) else: print("Error: the...
VERY URGENT*** THANK YOU IN ADVANCE: THIS IS THE CODE I HAVE GOTTEN SO FAR: PYTHON This is a code that is supposed to help someone study/ practice for jeopardy. The two functions described below are required for this assignment. You may add other functions that you think are appropriate: menu() This function, which displays all the user options to the screen, prompt the user for their choice and returns their choice. This function will verify user input and ALWAYS...
Create a new program in Mu and save it as ps4.5.2.py and take the code below and fix it as indicated in the comments: # Do not change the line of code below. It's at the top of # the file to ensure that it runs before any of your code. # You will be able to access french_dict from inside your # function. french_dict = {"me": "moi", "hello": "bonjour", "goodbye": "au revoir", "cat": "chat", "dog": "chien", "and": "et"} #...
Storing data and adding to data in a dictionary: Experiment with Python dictionaries and their keys() member function that returns a list of a dictionary's keys. You can check if a particular key is in a dictionary by using the in test with the keys list. For example, if our dictionary is MyDict = { 1: 'One', 2: 'Two', 3: 'Three' } Then ( 2 in MyDict.keys() ) evaluates to True, indicating that 2 is a valid key for MyDict....
Use python
Start:
def main():
gradeList = []
fileName = getFile()
gradeList = getData(fileName)
mean = calculateMean(gradeList)
sd = calculateSD(mean, gradeList)
displayHeadings(gradeList, mean, sd)
curveGrades(mean, sd, gradeList)
if __name__ == "__main__":
main()
For this program, you will create a grade curving program by reading grades from a file, calculating the mean and standard deviation. The mean is the average value of the grades and the standard deviation measures the spread or dispersal of the numbers from the...
23.4 Project 4: Using Pandas for data analysis and practice with
error handling
Python Please!
23.4 PROJECT 4: Using Pandas for data analysis and practice with error handling Overview In this project, you will use the Pandas module to analyze some data about some 20th century car models, country of origin, miles per gallon, model year, etc. Provided Input Files An input file with nearly 200 rows of data about automobiles. The input file has the following format (the same...
23.4 PROJECT 4: Using Pandas for data analysis and practice with error handling Overview In this project, you will use the Pandas module to analyze some data about some 20th century car models, country of origin, miles per gallon, model year, etc. Provided Input Files An input file with nearly 200 rows of data about automobiles. The input file has the following format (the same as what you had for your chapter 13 labs). The following is an example of...
This lab will combine reading data from a file and searching the
array to find a specific value.
Assignment
Write a program that reads in a file full of strings into an
array, and prompts the user for a string to find in the array. The
program should loop until a sentinel value (such as -1) is
entered.
After looping in main() for the input, write a search function,
with the following prototype:
int findWord(string [], int, string);
with arguments...