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
""" ''' Write a python code to push all zeors to the end of an array...
""" ''' Write a python code to push all zeors to the end of an array ''' import numpy as np def Move_a(i):    num = len(a)    for k in range (i, num-1): a[k] = a[k+1] a[num-1] = 0    return a a = np.array([0,1,4,7,0,9,12,0,0,15,0,21]) #length of array (len) num = len(a) print (num) for i in range(0,num): if (a[i] == 0): #Functioon call to Move_a() a = Move_a(i)       print ("the array looks like") print (a) My...
Fill in the code for the function below called containsAvgValue. It takes as input a float...
Fill in the code for the function below called containsAvgValue. It takes as input a float array of any length and returns 1 if the array happens to contain a value that equals the average of all the values in the array or 0 otherwise. For example, when give the array [2.0, 2.0] it should return 1 since the average value is 2.0 and the array contains that value. To help you, I’ve included a function that computes the average...
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 {...
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...
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):...
So, i have this code in python that i'm running. The input file is named input2.txt...
So, i have this code in python that i'm running. The input file is named input2.txt and looks like 1.8 4.5 1.1 2.1 9.8 7.6 11.32 3.2 0.5 6.5 The output2.txt is what i'm trying to achieve but when the code runs is comes up blank The output doc is created and the code doesn't error out. it should look like this Sample Program Output 70 - 510, [semester] [year] NAME: [put your name here] PROGRAMMING ASSIGN MENT #2 Enter...
Develop a Traceroute application in python using ICMP. Your application will use ICMP but, in order...
Develop a Traceroute application in python using ICMP. Your application will use ICMP but, in order to keep it simple, will not exactly follow the official specification in RFC 1739.. Below you will find the skeleton code for the client. You are to complete the skeleton code. The places where you need to fill in code are marked with #Fill in start and #Fill in end. Code from socket import * import os import sys import struct import time import...
# Parts to be completed are marked with '<<<<< COMPLETE' import random N = 8 MAXSTEPS...
# Parts to be completed are marked with '<<<<< COMPLETE' import random N = 8 MAXSTEPS = 5000 # generates a random n-queens board # representation: a list of length n the value at index i is # row that contains the ith queen; # exampe for 4-queens: [0,2,0,3] means that the queen in column 0 is # sitting in row 0, the queen in colum 1 is in row, the queen in column 2 # is in row 0,...
Program will allow anywhere between 1 and 6 players (inclusive). Here is what your output will...
Program will allow anywhere between 1 and 6 players (inclusive). Here is what your output will look like: Enter number of players: 2 Player 1: 7S 5D - 12 points Player 2: 4H JC - 14 points Dealer: 10D Player 1, do you want to hit? [y / n]: y Player 1: 7S 5D 8H - 20 points Player 1, do you want to hit? [y / n]: n Player 2, do you want to hit? [y / n]: y...
I am a beginner when it comes to java codeing. Is there anyway this code can...
I am a beginner when it comes to java codeing. Is there anyway this code can be simplified for someone who isn't as advanced in coding? public class Stock { //fields private String name; private String symbol; private double price; //3 args constructor public Stock(String name, String symbol, double price) { this.name = name; this.symbol = symbol; setPrice(price); } //all getters and setters /** * * @return stock name */ public String getName() { return name; } /** * set...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT