1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Helen [10]
3 years ago
5

The function below tries to create a list of integers by reading them from a file. The file is expected to have a single integer

per line and the code works correctly in that case. If any of the lines don't contain just an integer, than this code fails. Fix this code so that it ignores any lines that don't contain integers instead of raising an exception. Do this with try/except blocks. Your code should still return the list of integers from the lines that contain integers.

Computers and Technology
1 answer:
Setler [38]3 years ago
4 0

Answer:

Check the explanation

Explanation:

def get_list_of_integers_from_file(filename):

int_list=[]

for line in open(filename).readlines():

try:

int_list.append(int(line))

except:

continue

return int_list

print(get_list_of_integers_from_file('file.txt'))

 

File.txt:

Kindly check the output below.

You might be interested in
In Python, if var1 = "Happy" and var2= "Birthday" and var3 = (var 1+var2) *2, then var3 stores the string
nordsb [41]

Answer:

The answer is "Option A".

Explanation:

The program to the given question can be given as:

program:

var1 = "Happy" #defining variable var1

var2= "Birthday" #defining variable var2

var3 = (var1+var2) *2 #defining variable var3 and calculate value

print (var3) #print value.

Output:

HappyBirthdayHappyBirthday

In the above python program, three variable is defined, that is var1, var2, and var3, in which variable var1 and var2 we assign a value, that is "Happy" and "Birthday".

In variable var3 we add the value of var1 and var2 variable and multiply by 2. It will print the variable value two times. and other options are not correct that can be defined as:

  • In option B and C, Both variable var1 and var2 print there values two times but in option B var1 value print two time and var2 value print one time only and option C var1 variable value is print only one time and var2 variable value is print two times that's why it is not correct.
  • In option C, Both variable var1 and var2 print there values two times that's why it is not correct.

7 0
2 years ago
The difference between Return key and word Wrap
saveliy_v [14]
The enter key is used when we want to go directly to the next line while writing whereas through word wrap the cursor automatically shifts to the next line when the word limit for a particular line is exceeded
3 0
2 years ago
What's the difference between help desk and service desk?
Mekhanik [1.2K]

Answer:

A help desk is considered to be focused on break-fix (like a incident management), where a service desk is there to assist with break-fix and with service requests (requests for new services) and requests for information (ex. “how do I do <u>X</u>?”).

Hope this helps!

3 0
2 years ago
The birthday paradox says that the probability that two people in a room will have the same birthday is more than half, provided
poizon [28]

Answer:

The Java code is given below with appropriate comments for explanation

Explanation:

// java code to contradict birth day paradox

import java.util.Random;

public class BirthDayParadox

{

public static void main(String[] args)

{

   Random randNum = new Random();

   int people = 5;

   int[] birth_Day = new int[365+1];

   // setting up birthsdays

   for (int i = 0; i < birth_Day.length; i++)

       birth_Day[i] = i + 1;

 

   int iteration;

   // varying number n

   while (people <= 100)

   {

       System.out.println("Number of people: " + people);

       // creating new birth day array

       int[] newbirth_Day = new int[people];

       int count = 0;

       iteration = 100000;

       while(iteration != 0)

       {

           count = 0;

           for (int i = 0; i < newbirth_Day.length; i++)

           {

               // generating random birth day

               int day = randNum.nextInt(365);

               newbirth_Day[i] = birth_Day[day];

           }

           // check for same birthdays

           for (int i = 0; i < newbirth_Day.length; i++)

           {

               int bday = newbirth_Day[i];

               for (int j = i+1; j < newbirth_Day.length; j++)

               {

                   if (bday == newbirth_Day[j])

                   {

                       count++;

                       break;

                   }

               }

           }

           iteration = iteration - 1;

       }

       System.out.println("Probability: " + count + "/" + 100000);

       System.out.println();

       people += 5;

   }

}

}

4 0
3 years ago
Lenovo laptop
igor_vitrenko [27]
Oh i do i do i do .... but could u retake the pic. it is kinda blurry
4 0
3 years ago
Other questions:
  • Instructions:Select the correct answer.
    13·1 answer
  • Assume that a kernel is launched with 1000 thread blocks each of which has 512 threads. If a variable is declared as a shared me
    6·1 answer
  • What is technology addiction​
    14·1 answer
  • andy accidentally saved a file in the wrong folder. what is the quickest way to move the file? create a new file and save it in
    8·2 answers
  • What mass of nh3 can be made from 35g of n2?
    14·1 answer
  • True or false: within a database, fields can be added, deleted and edited but never moved.
    11·1 answer
  • What is a GUI?
    11·1 answer
  • You should check your battery ___________. Every week Never Every six months Every 30,00 miles
    8·2 answers
  • Complete the method/function so that it converts dash/underscore delimited words into camel casing. The first word within the ou
    10·1 answer
  • Given the three thread states: running, runnable (i.e., ready), and blocked (i.e., waiting), state which of the six possible thr
    13·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!