The following program has a bug. What is it? def get data(ifile): myList = ] %3D for line in ifile: myList.append(line.rstrip() ifile.close() return myList def main(): inFile = open("data.txt", 'r') %3D get_data(inFile) for i in range(len(myList)): print(list[i])

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question
The following program has a bug. What is it?
def get data(ifile):
myList = 0
for line in ifile:
myList.append(line.rstrip()
ifile.close()
return myList
def main():
inFile = open("data.txt", 'r')
get_data(inFile)
for i in range(len(myList)):
print(list[i])
main()
O main() does not save the list that get_data() returns; instead it tries to access myList, which is local to get_data() and not defined in main().
In get_data(), the code is trying to append a string to a list.
OIn get_data(), the for-loop doesn't read anything from the file.
O get_data() strips newlines from the end of each line, so main() will print all the data on one line.
O The main() function should not open data.text; it should pass the filename to get_data().
Transcribed Image Text:The following program has a bug. What is it? def get data(ifile): myList = 0 for line in ifile: myList.append(line.rstrip() ifile.close() return myList def main(): inFile = open("data.txt", 'r') get_data(inFile) for i in range(len(myList)): print(list[i]) main() O main() does not save the list that get_data() returns; instead it tries to access myList, which is local to get_data() and not defined in main(). In get_data(), the code is trying to append a string to a list. OIn get_data(), the for-loop doesn't read anything from the file. O get_data() strips newlines from the end of each line, so main() will print all the data on one line. O The main() function should not open data.text; it should pass the filename to get_data().
The following input validation code has a bug. What is it? (Line numbers are not part of the code.)
1. again
= True
2. while again:
3.
try:
4.
userChoice = int(input("Please enter your choice (1-5): ")
%3D
5.
if userChoice >= 1 or userChoice <= 5:
6.
again
= False
7.
else:
8.
print("That is not a valid number.")
9.
except ValueError:
10.
print("That is not a number.")
In line 9, the except statement should say except IntError.
O Line 1 should be again = False.
O In line 5, the or should be an and.
O Line 4 should not convert to an int.
In line 6, again = False should be again == False.
Transcribed Image Text:The following input validation code has a bug. What is it? (Line numbers are not part of the code.) 1. again = True 2. while again: 3. try: 4. userChoice = int(input("Please enter your choice (1-5): ") %3D 5. if userChoice >= 1 or userChoice <= 5: 6. again = False 7. else: 8. print("That is not a valid number.") 9. except ValueError: 10. print("That is not a number.") In line 9, the except statement should say except IntError. O Line 1 should be again = False. O In line 5, the or should be an and. O Line 4 should not convert to an int. In line 6, again = False should be again == False.
Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY