The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | Apr 2017 |
| Last Sign in: | 103 Weeks Ago, 2 Days Ago |
| Questions Answered: | 4870 |
| Tutorials Posted: | 4863 |
MBA IT, Mater in Science and Technology
Devry
Jul-1996 - Jul-2000
Professor
Devry University
Mar-2010 - Oct-2016
I am trying to do this.
Question 1: File Number Check with exceptions: Write a function that reads numbers from a file, and then checks if they are integers, floats, and/or all digits. The program should ask the user for the name of the input file. The program should also handle the possibility that the input file does not exist.
The input file is formatted as a single number or string per line.
Your output should be original_data + “t” + is_or_not_int + “t” + is_or_not_float+ “t” + is_or_not_all_digit + “n”.
Sample Shell output:
>>>
Enter the name of the Integer File: Numbers.txt
>>>
Sample Input File:
2008
22.4533
Hello
2000
170.242
180.0
1900
0
Hello World
Sample Output File:
2008Is_IntIs_FloatIs_All_Digit
22.4533Not_IntIs_FloatNot_All_Digit
HelloNot_IntNot_FloatNot_All_Digit
2000Is_IntIs_FloatIs_All_Digit
170.242Not_IntIs_FloatNot_All_Digit
180.0Not_IntIs_FloatNot_All_Digit
1900Is_IntIs_FloatIs_All_Digit
0Is_IntIs_FloatIs_All_Digit
Hello WorldNot_IntNot_FloatNot_All_Digit
Here's my code so far.
def check_int(i):
try:
int(i)
i.write('Is_Int')
except ValueError:
i.write('tNot_Int')
def check_float(i):
try:
float(i)
i.write('tIs_float ')
except ValueError:
i.write('tNot_float')
def check_is_digit(i):
try:
int(i)
i.write('tIs_all_Digit')
except ValueError:
i.write('tNot_all_digit')
def open_file():
x = input('Enter the file name: ')
try:
y = open(x,'w')
for i in y.readlines():
i = i.split()
check_int(i)
check_float(i)
check_is_digit(i)
y.close()
except FileNotFoundError:
print('wrong file, please try again. ')
open_file()