Question

convert this code to accept int value instead of float values using python. Make sure to...

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()

Homework Answers

Answer #1

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.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
This is my code, python. I have to search through the roster list to find a...
This is my code, python. I have to search through the roster list to find a player using their number. it says list index out of range. it also says there is error in my main. def file_to_dictionary(rosterFile): myDictionary={}       with open(rosterFile,'r') as f: data=f.read().split('\n')       for line in data:    (num,first,last,position)=line.split() myDict=[first, last, position] myDictionary[num]=myDict print (myDictionary) return myDictionary file_to_dictionary((f"../data/playerRoster.txt"))    def find_by_number(number): player=None    second=[] foundplayer= False myDictionary=file_to_dictionary((f"../data/playerRoster.txt")) for p in myDictionary: fullplayer=p.split() second.append([fullplayer[0], (fullplayer[1]+" "+...
In Python def main(): c = 10 print("Welcome to Roderick's Chikkin and Gravy") e = False...
In Python def main(): c = 10 print("Welcome to Roderick's Chikkin and Gravy") e = False while not e: x = input("Would you like some chikkin?") if x == "Y" or x == "y": c = f(c) else: e = True if c == 0: e = True print("I hope you enjoyed your chikkin!") def f(c): if c > 0: print("Got you some chikkin! Enjoy") return c-1 else: print("No chikkin left. Sorry") return 0 main() For practice with lists, create...
I am a student taking python programming. Can this problem be modified using the define main...
I am a student taking python programming. Can this problem be modified using the define main method, def main()? import random #function definition #check for even and return 0 if even def isEven(number): if(number%2==0): return 0 #return 1 if odd else: return 1 #count variables even =0 odd = 0 c = 0 #loop iterates for 100 times for i in range(100): #generate random number n = random.randint(0,1000) #function call val = isEven(n) #check value in val and increment if(val==0):...
Build two arrays[ ] (Integer and String) and convert them to two ArrayLists and write two...
Build two arrays[ ] (Integer and String) and convert them to two ArrayLists and write two overloaded generic static search method to find the index locations of a specified value. One of the search methods applies to the array type while the other (overloaded) search method applies to the collection type. Implement the following generic linear search method and write a client program to display results: (Here is the header) public static <E extends Comparable<E>> int search(E[] list, E key)...
Python Blackjack Game Here are some special cases to consider. If the Dealer goes over 21,...
Python Blackjack Game Here are some special cases to consider. If the Dealer goes over 21, all players who are still standing win. But the players that are not standing have already lost. If the Dealer does not go over 21 but stands on say 19 points then all players having points greater than 19 win. All players having points less than 19 lose. All players having points equal to 19 tie. The program should stop asking to hit if...
This is an intro to Python question: #Write a function called linear() that takes two parameters...
This is an intro to Python question: #Write a function called linear() that takes two parameters #- a list of strings and a string. Write this function so #that it returns the first index at which the string is #found within the list if the string is found, or False if #it is not found. You do not need to worry about searching #for the search string inside the individual strings within #the list: for example, linear(["bobby", "fred"], "bob") #should...
Create an add method for the BST (Binary Search Tree) class. add(self, new_value: object) -> None:...
Create an add method for the BST (Binary Search Tree) class. add(self, new_value: object) -> None: """This method adds new value to the tree, maintaining BST property. Duplicates must be allowed and placed in the right subtree.""" Example #1: tree = BST() print(tree) tree.add(10) tree.add(15) tree.add(5) print(tree) tree.add(15) tree.add(15) print(tree) tree.add(5) print(tree) Output: TREE in order { } TREE in order { 5, 10, 15 } TREE in order { 5, 10, 15, 15, 15 } TREE in order {...
Complete the java code as per the comments public class Sorting {    ///////////////////////////////////////////////    //...
Complete the java code as per the comments public class Sorting {    ///////////////////////////////////////////////    // STEP 1 -- Make sorting methods generic    ///////////////////////////////////////////////       /**    * Re-orders the contents given array using the insertion sort algorithm.    *    * @param data The array to be sorted.    */    //TODO: Make me generic to work on any kind of Comparable data!    public static void insertionSort(int[] data)    {        int insert; // temporary...
Please create a python module named homework.py and implement the functions outlined below. Below you will...
Please create a python module named homework.py and implement the functions outlined below. Below you will find an explanation for each function you need to implement. When you are done please upload the file homework.py to Grader Than. Please get started as soon as possible on this assignment. This assignment has many problems, it may take you longer than normal to complete this assignment. This assignment is supposed to test you on your understanding of reading and writing to a...
Python 3 Rewrite KNN sample code using KNeighborsClassifier . ● Repeat KNN Step 1 – 5,...
Python 3 Rewrite KNN sample code using KNeighborsClassifier . ● Repeat KNN Step 1 – 5, for at least five times and calculate average accuracy to be your result. ● If you use the latest version of scikit -learn, you need to program with Python >= 3.5. ● Use the same dataset: “ iris.data ” ● Split your data: 67% for training and 33% for testing ● Draw a line chart: Use a “for loop” to change k from 1...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT