Question

VERY URGENT*** THANK YOU IN ADVANCE: THIS IS THE CODE I HAVE GOTTEN SO FAR: PYTHON...

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 return a valid choice. addCapital(dict) This function accepts a dictionary as a parameter. This dictionary contains store country:capital pairs. The function prompts the user for a country. If the country is not already a key in this dictionary, the user is prompted for a capital and the pair is added to the dictionary. If the country already exists in the dictionary, the user is informed that the capital is already stored and no change is made to the dictionary.

THE MAIN PROCESS: Add a country and it’s capital to the dictionary. The addCapital function is to be called to complete this task. Both country and capital should be converted to first letter capitalized only form. That is, if the country is entered as enGLand, it is stored as England. The string function title is helpful here. The user can enter a country, and if that country is in the dictionary, it’s capital is printed, otherwise an error message is printed. Keep in mind that the data is stored as first letter cap only, so any user country must be converted to this form before the program looks to see if it is in the dictionary. Print out the number of countries currently represented in the dictionary. Print out all the countries which are currently represented in the dictionary. Print out all of the countries and their capitals currently stored. If there are no capitals currently stored in the dictionary, a message is printed Exit the program

CODE:

def menu(): #prints the options
print("Options\n1. Add a country and it’s capital to the dictionary")
print("2. Print Captial of a country")
print("3. Print out the number of countries currently represented in the dictionary")
print("4. Print out all the countries which are currently represented in the dictionary")
print("5. Exit")
n=0 #stop loop after user enters valid input
while n<1 or n>5:
n=int(input("Please enter a valid option: ")) #return n
return n
def addCapital(Countries): #get the name of the country from the user
country=input("Enter the name of the country: ") #convert into standard title form
country=country.title() #if country is already in the dict, do not change the dictionary
if country in Countries:
print(country," is already in Dictionary, No change is made to dictionary")
else: #otherwise, get the capital from the user
capital=input("Enter the name of the captial: ") #convert into title format
capital=capital.title() #add to dictionary
Countries[country]=capital


import country_help #import the helping module, and creates an empty dictionary
countries_dict={} #while in loop this stops when menu() sends 5
while True: #get the user choice
choice=country_help.menu() #if choice is 1, call addCapital function
if choice==1:
country_help.addCapital(countries_dict)
elif choice==2: #if choice is 2, get the title from the user
c=input("Enter the name of the country: ")
c=c.title() #if country exists in the dictionary, print the capital
if c in countries_dict:
print("Capital of ",c," is ",countries_dict[c])
else: #if not then print error message
print(c," is not found in the dictonary")
elif choice==3: #if choice is 3, print the length of the dictionary
print("Number of countries in the dictionary is: ",len(countries_dict))
elif choice==4: #if choice is 4 print the dictionary contents
for i in countries_dict:
print(i," : ",countries_dict[i])
else: #break if choice is 4
break

my problem is that when I try to run this code, and type in option 1 (add a country and it’s capital to the dictionary) it gives me an error saying

"AttributeError: module 'country_help' has no attribute 'addCapital'"

Not sure why, where/what is the problem ... how can this be fixed?

thank you....

0 0
Add a comment Improve this question Transcribed image text
Answer #1


ANSWER:-

CODE:-

Save below code using whatever name you want.

import country_help #import the helping module, and creates an empty dictionary
countries_dict={} #while in loop this stops when menu() sends 5
while True: #get the user choice
    choice=country_help.menu() #if choice is 1, call addCapital function
    if choice==1:
        country_help.addCapital(countries_dict)
    elif choice==2: #if choice is 2, get the title from the user
        c=input("Enter the name of the country: ")
        c=c.title() #if country exists in the dictionary, print the capital
        if c in countries_dict:
            print("Capital of ",c," is ",countries_dict[c])
        else: #if not then print error message
            print(c," is not found in the dictonary")
    elif choice==3: #if choice is 3, print the length of the dictionary
        print("Number of countries in the dictionary is: ",len(countries_dict))
    elif choice==4: #if choice is 4 print the dictionary contents
        for i in countries_dict:
            print(i," : ",countries_dict[i])
    else: #break if choice is 4
        break


country_help.py:-

Save below code in country_help.py and make sure both files should be in same directory.

def menu(): #prints the options
    print("Options\n1. Add a country and it’s capital to the dictionary")
    print("2. Print Captial of a country")
    print("3. Print out the number of countries currently represented in the dictionary")
    print("4. Print out all the countries which are currently represented in the dictionary")
    print("5. Exit")
    n=0 #stop loop after user enters valid input
    while n<1 or n>5:
        n=int(input("Please enter a valid option: ")) #return n
    return n
def addCapital(Countries): #get the name of the country from the user
    country=input("Enter the name of the country: ") #convert into standard title form
    country=country.title() #if country is already in the dict, do not change the dictionary
    if country in Countries:
        print(country," is already in Dictionary, No change is made to dictionary")
    else: #otherwise, get the capital from the user
        capital=input("Enter the name of the captial: ") #convert into title format
    capital=capital.title() #add to dictionary
    Countries[country]=capital

NOTE:- I have not changed anything in the code. I just saved code in seperate files and run. I got the output.

NOTE:- If you need any modifications in the code,please comment below.Please give positive rating.THUMBS UP.

        THANK YOU!!!!

OUTPUT:-

country_help.py:-

Add a comment
Know the answer?
Add Answer to:
VERY URGENT*** THANK YOU IN ADVANCE: THIS IS THE CODE I HAVE GOTTEN SO FAR: PYTHON...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • For Python 3.8! Copy and paste the following bolded code into IDLE: movies = [[1939, 'Gone...

    For Python 3.8! Copy and paste the following bolded code into IDLE: movies = [[1939, 'Gone with the Wind', 'drama'],           [1943, 'Casablanca', 'drama'],           [1965, 'The Sound of Music', 'musical'],           [1969, 'Midnight Cowboy', 'drama'],           [1972, 'The Godfather', 'drama'],           [1973, 'The Sting', 'comedy'],           [1977, 'Annie Hall', 'comedy'],           [1982, 'Ghandi', 'historical'],           [1986, 'Platoon', 'action'],           [1990, 'Dances with Wolves', 'western'],           [1992, 'Unforgiven', 'western'],           [1994, 'Forrest Gump', 'comedy'],           [1995, 'Braveheart', 'historical'],           [1997,...

  • Need help writing this code in python. This what I have so far. def display_menu(): print("======================================================================")...

    Need help writing this code in python. This what I have so far. def display_menu(): print("======================================================================") print(" Baseball Team Manager") print("MENU OPTIONS") print("1 - Calculate batting average") print("2 - Exit program") print("=====================================================================")    def convert_bat(): option = int(input("Menu option: ")) while option!=2: if option ==1: print("Calculate batting average...") num_at_bats = int(input("Enter official number of at bats: ")) num_hits = int(input("Enter number of hits: ")) average = num_hits/num_at_bats print("batting average: ", average) elif option !=1 and option !=2: print("Not a valid...

  • Can you add code comments where required throughout this Python Program, Guess My Number Program, and...

    Can you add code comments where required throughout this Python Program, Guess My Number Program, and describe this program as if you were presenting it to the class. What it does and everything step by step. import random def menu(): #function for getting the user input on what he wants to do print("\n\n1. You guess the number\n2. You type a number and see if the computer can guess it\n3. Exit") while True: #using try-except for the choice will handle the...

  • python programming: Can you please add comments to describe in detail what the following code does:...

    python programming: Can you please add comments to describe in detail what the following code does: import os,sys,time sl = [] try:    f = open("shopping2.txt","r")    for line in f:        sl.append(line.strip())    f.close() except:    pass def mainScreen():    os.system('cls') # for linux 'clear'    print("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")    print(" SHOPPING LIST ")    print("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")    print("\n\nYour list contains",len(sl),"items.\n")    print("Please choose from the following options:\n")    print("(a)dd to the list")    print("(d)elete from the list")    print("(v)iew the...

  • NOTE: LANGUAGE IS PYTHON CS160 Computer Science Lab 17 Objectives Work with dictionaries Work with functions...

    NOTE: LANGUAGE IS PYTHON CS160 Computer Science Lab 17 Objectives Work with dictionaries Work with functions Work with files Overview This lab will have you store the birthdays for a group of people. This program will store the information with the focus on the date of the month and not on the individual. The point of this program is to be able to see who has a birthday on a given day or to create a table of birthdays, in...

  • NOTE: LANGUAGE IS PYTHON CS160 Computer Science Lab 17 Objectives Work with dictionaries Work with functions...

    NOTE: LANGUAGE IS PYTHON CS160 Computer Science Lab 17 Objectives Work with dictionaries Work with functions Work with files Overview This lab will have you store the birthdays for a group of people. This program will store the information with the focus on the date of the month and not on the individual. The point of this program is to be able to see who has a birthday on a given day or to create a table of birthdays, in...

  • Hello, In Python Enhance the prior assignment by doing the following 1) Create a class that...

    Hello, In Python Enhance the prior assignment by doing the following 1) Create a class that contain all prior functions MyLib # This function adds two numbers def addition(a, b): return a + b # This function subtracts two numbers def subtraction(a, b): return a - b # This function multiplies two numbers def multiplication(a, b): return a * b # This function divides two numbers # The ZeroDivisionError exception is raised when division or modulo by zero takes place...

  • I currently have a code that I have written that uses global variables. My professor wants...

    I currently have a code that I have written that uses global variables. My professor wants me to rewrite this code so that it uses the return function instead. I dont understand why because it works the same, but I am not great at using return and barely understand it. here is my code. This is in Python3 #Created an empty dictionary using variable #grocery_item grocery_item = {} #Created an empty list for using variable #grocery_history grocery_history = [] #This...

  • I am having trouble with my Python code in this dictionary and pickle program. Here is...

    I am having trouble with my Python code in this dictionary and pickle program. Here is my code but it is not running as i am receiving synthax error on the second line. class Student: def__init__(self,id,name,midterm,final): self.id=id self.name=name self.midterm=midterm self.final=final def calculate_grade(self): self.avg=(self.midterm+self.final)/2 if self.avg>=60 and self.avg<=80: self.g='A' elif self.avg>80 and self.avg<=100: self.g='A+' elif self.avg<60 and self.avg>=40: self.g='B' else: self.g='C' def getdata(self): return self.id,self.name.self.midterm,self.final,self.g CIT101 = {} CIT101["123"] = Student("123", "smith, john", 78, 86) CIT101["124"] = Student("124", "tom, alter", 50,...

  • [Using Python] I need my code given below to loop the user input, so that you...

    [Using Python] I need my code given below to loop the user input, so that you are asked to input without it stopping after 4 inputs. Code: #A program that converts a hexadecimal number to its decimal value #Define function for hexadecimal to decimal def hexToDec(hexi): result = 0 #For loop to test input for correct values for char in hexi: if 'A' <= char <= 'F' or 'a' <= char <= 'f': if 'a' <= char <= 'f': result...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT