Question

I am stuck on how to increase and decrease the population of il in the below...

I am stuck on how to increase and decrease the population of il in the below question using Python 3:

import logging

class State:
    """A class representing a US state."""

    def __init__(self, name, postalCode, population):
        self.name = name     
        self.postalCode = postalCode
        self.population = population

    def __str__(self):
        """Readable version of the State object"""
        return 'Name: ' + self.name + ", Population (in MM): " + str(self.population) + ", PostalCode: " + self.postalCode

    def increase_population(self, numPeople):
        """Increases the population of the state."""
        self.population += numPeople
        print("Population of",self.name,"increased to",self.population,"million.")

    def decrease_population(self, numPeople):
        """Decreases the population of the state."""
        try:
            if (numPeople > self.population):
                raise ValueError("decrease_population(self, numPeople): Invalid value for population reduction")
            else:
                # TODO: decrease the population by the value in variable numPeople
                self.population -= numPeople

                # Print new population
                print("Population of",self.name,"decreased to",self.population,"million.")
        except Exception as e:
            logging.exception(e)  

il = State("Illinois","IL",12.8)

# TODO: increase the population of il by 1 million
#HELP HERE PLEASE

# TODO: decrease the population of il by 1.5 million
#HELP HERE PLEASE
0 0
Add a comment Improve this question Transcribed image text
Answer #1

import logging

class State:

def __init__(self, name, postalCode, population):

self.name = name

self.postalCode = postalCode

self.population = population

def __str__(self):

"""Readable version of the State object"""

return 'Name: ' + self.name + ", Population (in MM): " + str(self.population) + ", PostalCode: " + self.postalCode

def increase_population(self, numPeople):

"""Increases the population of the state."""

self.population += numPeople

print("Population of",self.name,"increased to",self.population,"million.")

def decrease_population(self, numPeople):

"""Decreases the population of the state."""

try:

if (numPeople > self.population):

raise ValueError("decrease_population(self, numPeople): Invalid value for population reduction")

else:

# TODO: decrease the population by the value in variable numPeople

self.population -= numPeople

# Print new population

print("Population of",self.name,"decreased to",self.population,"million.")

except Exception as e:

logging.exception(e)

il = State("Illinois","IL",12.8)

il.increase_population(1000000)

il.decrease_population(1500000)

import logging class State: def __init_(self, name, postalCode, population): self.name = name self.postal Code = postal Code

Add a comment
Know the answer?
Add Answer to:
I am stuck on how to increase and decrease the population of il in the below...
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
  • I am currently facing a problem with my python program which i have pasted below where...

    I am currently facing a problem with my python program which i have pasted below where i have to design and implement python classes and record zoo database and takes user input as query. the error i am recieving says that in line 61 list index is out of range. kindly someone help me with it as soon as possible. Below is the program kindly check and correct it. Thanks! class Animal: def __init__(self, name, types, species, mass): self.name=name self.type=types...

  • I am currently facing a problem with my python program which i have pasted below where i have to design and implement python classes and record zoo database and takes user input as query. the error i...

    I am currently facing a problem with my python program which i have pasted below where i have to design and implement python classes and record zoo database and takes user input as query. the error i am recieving says that in line 61 list index is out of range. kindly someone help me with it as soon as possible. Below is the program kindly check and correct it. Thanks! class Animal: def __init__(self, name, types, species, mass): self.name=name self.type=types...

  • Use your Food class, utilities code, and sample data from Lab 1 to complete the following...

    Use your Food class, utilities code, and sample data from Lab 1 to complete the following Tasks. def average_calories(foods):     """     -------------------------------------------------------     Determines the average calories in a list of foods.     foods is unchanged.     Use: avg = average_calories(foods)     -------------------------------------------------------     Parameters:         foods - a list of Food objects (list of Food)     Returns:         avg - average calories in all Food objects of foods (int)     -------------------------------------------------------     """ your code here is the...

  • PYTHON. Continues off another code(other code is below). I don't understand this. Someone please help! Comment...

    PYTHON. Continues off another code(other code is below). I don't understand this. Someone please help! Comment the lines please so I can understand. There are short and med files lengths for each the list of names/ids and then search id file. These are the input files: https://codeshare.io/aVQd46 https://codeshare.io/5M3XnR https://codeshare.io/2W684E https://codeshare.io/5RJwZ4 LinkedList ADT to store student records(code is below). Using LinkedList ADT instead of the Python List. You will need to use the Student ADT(code is below) Imports the Student class...

  • PYTHON. Continues off another code. I don't understand this. Someone please help! Comment the lines please...

    PYTHON. Continues off another code. I don't understand this. Someone please help! Comment the lines please so I can understand LinkedList ADT: class myLinkedList:     def __init__(self):         self.__head = None         self.__tail = None         self.__size = 0     def insert(self, i, data):         if self.isEmpty():             self.__head = listNode(data)             self.__tail = self.__head         elif i <= 0:             self.__head = listNode(data, self.__head)         elif i >= self.__size:             self.__tail.setNext(listNode(data))             self.__tail = self.__tail.getNext()         else:             current = self.__getIthNode(i - 1)             current.setNext(listNode(data,...

  • you will implement the A* algorithm to solve the sliding tile puzzle game. Your goal is...

    you will implement the A* algorithm to solve the sliding tile puzzle game. Your goal is to return the instructions for solving the puzzle and show the configuration after each move. A majority of the code is written, I need help computing 3 functions in the PuzzleState class from the source code I provided below (see where comments ""TODO"" are). Also is this for Artificial Intelligence Requirements You are to create a program in Python 3 that performs the following:...

  • 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,...

  • In this part, you will complete the code to solve a maze.

    - Complete the code to solve a maze- Discuss related data structures topicsProgramming-----------In this part, you will complete the code to solve a maze.Begin with the "solveMaze.py" starter file.This file contains comment instructions that tell you where to add your code.Each maze resides in a text file (with a .txt extension).The following symbols are used in the mazes:BARRIER = '-' # barrierFINISH = 'F' # finish (goal)OPEN = 'O' # open stepSTART = 'S' # start stepVISITED = '#' #...

  • In this project, you will construct an Object-Oriented framework for a library system. The library must...

    In this project, you will construct an Object-Oriented framework for a library system. The library must have books, and it must have patrons. The patrons can check books out and check them back in. Patrons can have at most 3 books checked out at any given time, and can only check out at most one copy of a given book. Books are due to be checked back in by the fourth day after checking them out. For every 5 days...

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
ADVERTISEMENT