Describe how catching exceptions can help with file errors. Write three Python examples that actually generate file errors on your computer and catch the errors with try: except: blocks. Include the code and output for each example in your post.
Describe how you might deal with each error if you were writing a large production program. These descriptions should be general ideas in English, not actual Python code.
Comment your code to demonstrate that you know what you are talking about!
We've already tried comparing 3 numbers to see the largest among all, so let's try a more complicated mission where we locate the position of the largest among 5 numbers. There are only 4 possible cases for this mission:
- if the largest digit is the first digit, print "Leftmost"
- if the largest digit is the third digit, print "Middle"
- if the largest digit is the last digit, print "Rightmost"
- if none of the above is correct, print "Unknown"
Now, show me how far you've understood your lessons!
Input
A line containing a five-digit integer.
1·4·6·3·2
Output
A line containing a string.
Middle
I'm quite into games now, so how about we play In or Out! When a given number is even, the team scores so we shall print "In", but if it's not an even number, then we print "Out". Simple, right?
Now let's get this game started.
Input
A line containing an integer.
35
Output
A line containing a string.
Out
Character Frequency
Write a program to compute the frequency of characters other than space.
Sample Input 1
Pop up
Sample Output 1
o: 1
p: 3
u: 1
Prefix suffix
Write a program to check the overlapping of one string's suffix with the prefix of another string.
Sample Input 1
ramisgood
goodforall
Sample output 1
good
Sample Input 2
finally
restforall
Sample Input 2
No overlapping
Polynomial
Given polynomial, write a program that prints polynomial in Cix^Pi + Ci-1x^Pi-1 +....+C1x+C0 format
Sample Input 1
5
0 2
1 3
2 1
4 7
3 6
Sample Input 1
7x^4 + 6x^3 + x^2 + 3x + 2
Numbers in string-2
Given a string, write a program to return the sum and average of the numbers that appear in the string, ignoring all other characters.
Sample Input 1
I am 25 years and 10 months old
Sample output 1
35
17.5
Sample input 2
Tech Foundation 35567
Sample output 2
35567
35567.0
Numbers in string-1
Given a string, write a program to return the sum and average of the digits of all numbers that appear in the string, ignoring all other characters.
Sample Input 1
Anjali25 is python4 Expert
Sample output 1
11
3.67
Sample input 2
Tech Foundation 35567
Sample output 2
26
5.2
Weekends
Given two dates D1 and D2,write a program to count the number of Saturday's and Sundays from D1 to D2(including D1 and D2). The date in string format is like "8 Feb 2021".
Input
The first line of input will contain date D1 in the string format.
The second line of input will contain date D2 in the string format.
Sample input 1
25 Jan 2021
14 Feb 2021
Sample output 1
Saturday: 3
Sunday: 3
Sample input 2
25 May 2019
22 Dec 2021
Sample output 2
Saturday:135
Sunday: 135
determine what's wrong in this code. why x is not defined?
x.field_names = [ "City name", "Area", "Population", "Annual Rainfall"]
list1 = [
["Adelaide", 1295, 1158259, 600.5],
["Brisbane'', 5905, 1857594, 1146, 4],
[ "Darwin", 112, 120900, 1714, 7],
["Hobart", 135, 20556, 619.5],
["Sydney", 2058, 4336374, 1214.8],
["Melbourne", 1566, 3806092, 646.9],
["Perth", 5386, 1554769, 869.4]]
for i in list1:
x.add_row(i)