Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

PYTHON #This script asks the user to input the name of a file, a number of chara

ID: 3676080 • Letter: P

Question

PYTHON

#This script asks the user to input the name of a file, a number of character,
#open, print it to the screen based on number of character, and closes the file

print("How many characters should I display at a time?")
char = int(input("> "))

while True:
print("What file should I display?")
filename = input("> ")
try:
with open(filename) as file:
while True:
character = file.read(char)
if character == "":
break
else:
print(character)
break
except IOError:
print("I couldn't read from that file. Please try a different filename")

How can I improve this so that it also checks to make sure the number the user enters is a positive integer, and repeatedly asks the user to enter a number until they do so properly. Should use try-except to make sure the user's input is in the form of the integer. (Allowed to use an if statement to make sure that the number is positive but can't use .isnumeric() or .isdigit() )

Example:

How many characters should I display at a time? five Please enter a whole number How many characters should I display at a time? 5.1 Please enter a whole number How many characters should I display at a time? Please enter a number bigger than 0 How many characters should I display at a time? Please enter a number bigger than 0 How many characters should I display at a time? What file should I display? > hanlon's razo I couldn't read from that file. Please try a different filename What file should I display? > hanlonsrazor I couldn't read from that file. Please try a different filename What file should I display? >hanlon I couldn't read from that file. Please try a different filename What file should I display? > hanlon.txt Never attr ibute to m alice that whic h is adequ ately

Explanation / Answer

I have updated this python code with type checking using data type checing manually..and used the try and excert calls for handling value error.

See the below updated code..

#This script asks the user to input the name of a file, a number of character,
#open, print it to the screen based on number of character, and closes the file
print("How many characters should I display at a time?")
try:
char = int(input("> "))
   input_type = type(char)
   if input_type == "str":
print "Your string was %s." % char
elif input_type == "int":
print "Your integer was %d." % char
       if(char <0):
       print "enter positive interger :"
       char = int(input("> "))
elif input_type == "float":
print "Your float was %d." % char
except ValueError:
print "Not a number"
while True:
print("What file should I display?")
filename = input("> ")
try:
with open(filename) as file:
while True:
character = file.read(char)
if character == "":
break
else:
print(character)
break
except IOError:
print("I couldn't read from that file. Please try a different filename")