Question

In: Computer Science

Python 1.Explain what happens when a program receives a non-numeric string when a number is expected...

Python

1.Explain what happens when a program receives a non-numeric string when a number is expected as input, and explain how the try-except statement can be of use in this situation.

2.What is meant by the state of an object, and how does the programmer access and manipulate it?

3.Explain the differences between instance variables and temporary variables. Focus on their visibility in a class definition, and on their roles in managing data for an object of that class.

4.Explain the purpose of the variable self in a Python class definition.

Solutions

Expert Solution

Ans1:

  • In lots of Python programs we want to interact with the end-user by asking questions and retrieving user inputs from the user.In order to do so we generally use input() function.
  • We also have some cases in which we need to find numbers like whole numbers in order to achieve that we need to convert user input into integer using int() function.
  • But in the above case if the users provide non-numeric values then the conversion will fail using above method and exception will be generated and the program will stop running.
  • So, in order to validate the user entry and ensure that it is a numeric, we can use use try-except function.
  • The try block helps us to test the block of code for errors and the except block is used to handle the errors and finally block helps us to execute code, without depending on the result of try-except block.
  • For the above scenario try-except can be used like it will go on asking the user to provide a valid input then only the program can be executed.

Ans2:

  • State of an object have an hold within the static properties of the object including the current values of each of these properties .
  • Basically state of an object provides us the information about the type or value of that object. For example We have object called car and its state is colour,engine,wheels.
  • Programmers can easily modify the state of objects as Object oriented Programming has provided the feature of data encapsulation which means the act of representing essential features without showing background details
  • It tells that if any programmer has created an object then a new programmer can modify it but former programmer features still remain intact as new programmer only sees the outside details not the internal details .

Ans 3:

Instance Variables:-

  • They are declared in class but outside the method, constructor or block
  • The created when an object is created using the keyword ‘new’ and is destroyed when object is destroyed.
  • They can be accessed by calling the variable name inside class.
  • They hold values that are being referenced by one or more method,constructor or block

Temporary variables:-

  • They are represented as instances in order to call method or chain of methods.
  • They are immediately destroyed after invoking the call.
  • They are just used to invoke call method.
  • The values acquired by them are stored in variable rather than class interface.

Ans4:

  • The variable self represents the instance of the class.
  • When we use the “self” keyword we are able to access the attributes and methods of the class in python.
  • It helps to binds the attributes with the given arguments.
  • The most important reason we need to use self is because Python does not use the @ syntax to refer to instance attributes.
  • It helps to increase the readability of code.

Related Solutions

Python program. Write a function called cleanLowerWord that receives a string as a parameter and returns...
Python program. Write a function called cleanLowerWord that receives a string as a parameter and returns a new string that is a copy of the parameter where all the lowercase letters are kept as such, uppercase letters are converted to lowercase, and everything else is deleted. For example, the function call cleanLowerWord("Hello, User 15!") should return the string "hellouser". For this, you can start by copying the following functions discussed in class into your file: # Checks if ch is...
Python program.  from random import . Write a function called cleanLowerWord that receives a string as a...
Python program.  from random import . Write a function called cleanLowerWord that receives a string as a parameter and returns a new string that is a copy of the parameter where all the lowercase letters are kept as such, uppercase letters are converted to lowercase, and everything else is deleted. For example, the function call cleanLowerWord("Hello, User 15!") should return the string "hellouser". For this, you can start by copying the following functions discussed in class into your file: # Checks...
Given a string, such as x = ‘itm330’, write a Python program to count the number...
Given a string, such as x = ‘itm330’, write a Python program to count the number of digits and the number of letters in it. For example, in ‘itm330’, there are 3 letters and 3 digits. Hint: Very similar to page 11 on the slides. To check if a character c is a digit, use c.isdigit(). If c is a digit, c.isdigit() will be a True.
Programming Python Jupiter notebook Write a Python program that gets a numeric grade (on a scale...
Programming Python Jupiter notebook Write a Python program that gets a numeric grade (on a scale of 0-100) from the user and convert it to a letter grade based on the following table. The program should be written so that if the user entered either a non-numeric input or a numeric input out of the 0-100 range, it asks him/her to enter a numeric input in the correct range, instead of returning an error. Example: Enter your score: 78 Letter...
CREATE A NEW Java PROGRAM that demonstrates : 1.1 numeric and 1 string exception 2. Create...
CREATE A NEW Java PROGRAM that demonstrates : 1.1 numeric and 1 string exception 2. Create your own user defined exception, COMMENT it well, and add code so that it is thrown.
What happens when a router receives a packet for a network that isn't listed in the...
What happens when a router receives a packet for a network that isn't listed in the routing table? Select one: a. The router will use RIP to inform the host the packet is undeliverable. b. The router sends the packet to the next available router. c. The packet is held until routing table is updated with the required address. d. The packet will be discarded. Each input port of a router has a local copy of the router's forwarding table....
In Python Create a function called ????. The function receives a "string" that represents a year...
In Python Create a function called ????. The function receives a "string" that represents a year (the variable with this "String" will be called uve) and a list containing "strings" representing bank accounts (call this list ????). • Each account is represented by 8 characters. The format of each account number is "** - ** - **", where the asterisks are replaced by numeric characters. o For example, “59-04-23”. • The two central characters of the "string" of each account...
Python Explain what happens when a user clicks a command button in a fully functioning GUI...
Python Explain what happens when a user clicks a command button in a fully functioning GUI program.
Write a Python program that asks the user to enter a student's name and 8 numeric...
Write a Python program that asks the user to enter a student's name and 8 numeric tests scores (out of 100 for each test). The name will be a local variable. The program should display a letter grade for each score, and the average test score, along with the student's name. Write the following functions in the program: calc_average - this function should accept 8 test scores as arguments and return the average of the scores per student determine_grade -...
PYTHON Write a program that asks the user to enter a student's name and 8 numeric...
PYTHON Write a program that asks the user to enter a student's name and 8 numeric assignment scores (out of 100 for each assignment). The program should output the student's name, a letter grade for each assignment score, and a cumulative average for all the assignments. Please note, there are 12 students in the class so your program will need to be able to either accept data for 12 students or loop 12 times in order to process all the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT