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

  • class Bool(Expr): """A boolean constant literal. === Attributes === b: the value of the constant """...

    class Bool(Expr): """A boolean constant literal. === Attributes === b: the value of the constant """ b: bool def __init__(self, b: bool) -> None: """Initialize a new boolean constant.""" self.b = b # TODO: implement this method! def evaluate(self) -> Any: """Return the *value* of this expression. The returned value should the result of how this expression would be evaluated by the Python interpreter. >>> expr = Bool(True) >>> expr.evaluate() True """ return self.b def __str__(self) -> str: """Return a...

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

  • 9p This is for Python I need help. Pet #pet.py mName mAge class Pet: + __init__(name,...

    9p This is for Python I need help. Pet #pet.py mName mAge class Pet: + __init__(name, age) + getName + getAge0 def init (self, name, age): self.mName = name self.mAge = age Dog Cat def getName(self): return self.mName mSize mColor + __init__(name, age,size) + getSize() + momCommento + getDog Years() +_init__(name, age,color) + getColor + pretty Factor + getCatYears def getAge(self): return self.mAge #dog.py import pet #cat.py import pet class Dog (pet. Pet): class Cat (pet. Pet): def init (self,...

  • # Problem 3 def printexp(tree): sVal = "" if tree: sVal = '(' + printexp(tree.getLeftChild()) sVal = sV...

    # Problem 3 def printexp(tree): sVal = "" if tree: sVal = '(' + printexp(tree.getLeftChild()) sVal = sVal + str(tree.getRootVal()) sVal = sVal + printexp(tree.getRightChild())+')' return sVal #### Functions and classes to help with Homework 6 #### You should not make any changes to the functions and classes in this file, #### but you should understand what they do and how they work. import sys ### This function will be re-defined in hw6.py. However, it is needed for ExpTree, ###...

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

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