convert this code to accept int value instead of float values using python. Make sure to follow the same code. do not change the steps and make sure to point to what code you replaced. make sure to have 2 files
Method:-----------------------
#define a python user difined method
def get_float_val (prompt):
is_num = False
str_val = input (prompt) #prming read for our while
#while is_num == False: (ignore this but it works) old school
while not is_num:
try:
value = float(str_val)
is_num = True
except:
print (str_val + " is Not a float " )
str_val = input ( prompt )
#while
return value
#end get_float_val
#step 9 user defined method to test if a value is a float or not
def is_float (value):
try:
num = float(value)
return True # if we make it here, then the conversio was successful
except:
return False # conversion failed. It is not float
#end is_float
#step #15 find_string() method
# def means we are defining a method
# python does not have a return type
# 1st param list to search; 2nd param value to find
Method file:
def find_string(the_list, find_val):
list_len = len(the_list) #get the list size
found = False
i = 0
idx = -1 #where in the array was found; -1 = not found
#search the array for the value
while i < list_len and idx < 0: #equivelant to found == false
if find_val.upper() == the_list[i].upper():
idx = i
found = True
#end if
i += 1
#end while
return idx
#end find_string
#step 18
def find_int(the_list, find_val):
list_len = len(the_list) #get the list size
i = 0
idx = -1 #where is the array the value was found; -1 = not found
#search the array for the value
while i < list_len and idx < 0: #equivalent to found == false
if find_val == the_list[i]:
idx = i
#end if
i += 1
#end while
return idx
#end find_string
MethodTest: ------------------
import Method as m
def main():
#step 4 test our get_float_val() method
float_val = m.get_float_val( " Enter a floating point number: " )
print ( "Float value " +str( float_val ) + " was entered. ")
#step 10 test is_float()
str_val = input(" Enter a float value: ")
result = m.is_float( str_val )
#if result == True
if result:
print( str_val + " is a float " )
else:
print( str_val + " is not a float " )
#step #16 test find_string()
my_list = [ "goat", "zebra", "horse", "fox", "dog", "cat"]
value = input("Enter a value to find: ")
#call our method
index = m.find_string(my_list, value)
if index >= 0:
print ("Found " + value + " at index " + str(index) )
else:
print ( value + " NOT FOUND" )
#step #19 test find_int()
my_list = [3,5,7,9,11,13,15]
value = int(input( "Enter an integer to find: ") )
#call our method
index = m.find_int(my_list, value)
if index >= 0:
print( "Found " + str(value) + " at index " + str(index) )
else:
print( str(value) + " NOT FOUND!" )
#if this is the main module then execute main()
if __name__ == "__main__":
main()
The changes made are specified in the commentions of the code. Please follow them.
Method.py:
def find_string(the_list, find_val):
list_len = len(the_list) #get the list size
found = False
i = 0
idx = -1 #where in the array was found; -1 = not found
#search the array for the value
while i < list_len and idx < 0: #equivelant to found ==
false
if find_val.upper() == the_list[i].upper():
idx = i
found = True
#end if
i += 1
#end while
return idx
#end find_string
#step 18
def find_int(the_list, find_val):
list_len = len(the_list) #get the list size
i = 0
idx = -1 #where is the array the value was found; -1 = not
found
#search the array for the value
while i < list_len and idx < 0: #equivalent to found ==
false
if find_val == the_list[i]:
idx = i
#end if
i += 1
#end while
return idx
def get_int_val (prompt):
is_num = False
str_val = input (prompt) #prming read for our while
#while is_num == False: (ignore this but it works) old school
while not is_num:
try:
value = int(str_val)#changed here converting to int instaead of
float
is_num = True
except:
print (str_val + " is Not a float " )
str_val = input ( prompt )
#while
return value
def is_int (value):
try:
num = int(value)#changed here converting to int instaead of
float
return True # if we make it here, then the conversio was
successful
except:
return False
main.py:
import Method as m
def main():
#step 4 test our get_intt_val() method
int_val = m.get_int_val( " Enter a integer number: " )#method names
are modified from float to int
print ( "Integer value " +str( int_val ) + " was entered. ")
#step 10 test is_float()
str_val = input(" Enter a int value: ")
result = m.is_int( str_val )#method names are modified from float
to int
#if result == True
if result:
print( str_val + " is a integer " )#change message from float to
int
else:
print( str_val + " is not a integer " )
#step #16 test find_string()
my_list = [ "goat", "zebra", "horse", "fox", "dog", "cat"]
value = input("Enter a value to find: ")
#call our method
index = m.find_string(my_list, value)
if index >= 0:
print ("Found " + value + " at index " + str(index) )
else:
print ( value + " NOT FOUND" )
#step #19 test find_int()
my_list = [3,5,7,9,11,13,15]
value = int(input( "Enter an integer to find: ") )
#call our method
index = m.find_int(my_list, value)#method names are modified from
float to int
if index >= 0:
print( "Found " + str(value) + " at index " + str(index) )
else:
print( str(value) + " NOT FOUND!" )
#if this is the main module then execute main()
if __name__ == "__main__":
main()
Sample Output:
The screenshots are attached above for reference.
Please follow them for proper indentation and output.
Get Answers For Free
Most questions answered within 1 hours.