BHI503_Week5_Lab2.pdf BHI503 – eHealth Systems Lab #2: Python programming XXXXXXXXXXPoints Write a string-comparison function named strNcompare that compares up to n characters of a string s1 with the...

1 answer below »
PLEASE, THE QUESTION IS ON THE DOWNLOADED FILE


BHI503_Week5_Lab2.pdf BHI503 – eHealth Systems Lab #2: Python programming 3 Points Write a string-comparison function named strNcompare that compares up to n characters of a string s1 with the other string s2. The function must return 0, -1, or 1 if the first n-character portion of s1 is equal to, less than, or greater than the corresponding first n-character portion of s2, respectively. (1) You must use the following function definition: def strNcompare(s1, s2, n): (2) String comparison must be case-insensitive. For example, the strNcompare function must return 0 for the s1 value of ‘tommy’ and the s2 value of ‘TOMMY’. (3) You should not use a loop such as the while loop and the for loop inside the strNcompare function. Please see the ‘BHI503_Week5_Lab2_Example.py’ file about how to compare two strings without a loop. Then write a Python program that receives two words and the number of characters for comparison, calls the strNcompare function, and then shows the result. Your program keeps doing this until the user enters the value of -1. Please see the sample input and output below. Please test your program and make a screen shot enough to show that your program works correctly. Then submit your Python source code and screenshot. Your source code must be in the plain text py file format and your screenshot must be like the sample screenshot below. Below are the sample screenshot and its explanation: (1) Your program displays the message ‘String comparison [1(play), -1(quit)]: ’ and receives the value 1 or -1 from the user. (2) If the user enters the value -1, your program finishes immediately. (3) If the user enters the value 1, your program displays the message ‘Enter the first string: ’ and receives the first string (s1) for comparison from the user. (4) Then your program displays the message ‘Enter the second string: ’ and receives the second string (s2) for comparison from the user. (5) Also your program displays the message ‘Number of characters: ’ and receives the number (n) of characters for comparison from the user. (6) Finally, your program displays the result from comparing the first n characters of two strings s1 and s2 which the user entered. (7) Your program repeats this process. I have attached a simplified example and a supplementary document to help you with this assignment. BHI503_Week5_Lab2_Example.py def compare3chars(s): idx=3 val=0 if s[0:idx].lower()>"hello": val=1 return val play=2 while play>0: userString=input("Enter string: ") result=compare3chars(userString) print("Comparison of first 3 characters:",end=" ") if result==1: print("%s is greater than hello" %userString) else: print("%s is not greater than hello" %userString) play=play-1 BHI503_Week5_Lab2_ExampleDoc.pdf 1. Explanation for BHI503_Week5_Lab2_Example.py (1) Program The example program receives a string from user and compares the first 3 characters of the string (s) with a string 'hello' in the ‘compare3chars’ function. The assignment program must receive two strings and the number of characters from user, and compare the first n characters of the strings (s1 and s2) in the strNcompare function. (2) Number of characters In the case that the number of characters is 3, why does the assignment program output ‘tommy is equal to tomatos’? The assignment program must compare the first 3 characters of two strings. The first 3 characters of ‘tommy’ are ‘tom’ and the first 3 characters of ‘tomatos’ are ‘tom’. Therefore they are equal. (3) Function definition The function definition of the example program is def compare3chars(s): The function definition of the assignment program must be def strNcompare(s1, s2, n): (4) Function return value In the compare3chars function of the example program, if the first 3 character of s is greater than ‘hello’, the function returns 1. Otherwise, it returns 0. In the strNcompare function of the assignment program, the function must return 0, -1, or 1 if the first n-character portion of s1 is equal to, less than, or greater than the corresponding first n-character portion of s2, respectively. (5) Python while loop The example program iterates the while loop twice. The assignment program must repeat the process until the user enters the value of -1. 2. Implementation of the assignment (1) Structure The assignment program must receive two strings and the number of characters from user, call the strNcompare function, and then display the result on the basis of the value returned by the strNcompare function. The program keeps doing this until the user enters the value of -1. (2) Python input function Python input function receives a user input as a string, and so the int function may be used to convert it to an integer number. Please see the ‘BHI503_Week5_Lab2_Supplementary.pdf’ file. (3) Python print statement The following print statements may be useful for displaying the result: age1=25 age2=30 print("She is %s years old and he is %s years old" %(age1,age2)) print("She is {} years old and he is {} years old" .format(age1,age2)) print("She is {0} years old and he is {1} years old" .format(age1,age2)) 3. Submission (1) Python source code Submit the source code in the plain text py file format. (2) Screen shot Submit a screenshot image. BHI503_Week5_Lab2_Supplementary.pdf Exercise4.Createabasiccalculator. Programmakeasimplecalculatorthatcanadd,subtract,multiplyanddivide usingfunctions ''' Program make a simple calculator that can add, subtract, multiply and divide using functions ''' # This function adds two numbers def add(x, y): return x + y # This function subtracts two numbers def subtract(x, y): return x - y # This function multiplies two numbers def multiply(x, y): return x * y # This function divides two numbers def divide(x, y): return x / y print("Select operation.") print("1.Add") print("2.Subtract") print("3.Multiply") print("4.Divide") # Take input from the user choice = input("Enter choice(1/2/3/4):") num1 = int(input("Enter first number: ")) num2 = int(input("Enter second number: ")) if choice == '1': print(num1,"+",num2,"=", add(num1,num2)) elif choice == '2': print(num1,"-",num2,"=", subtract(num1,num2)) elif choice == '3': print(num1,"*",num2,"=", multiply(num1,num2)) elif choice == '4': print(num1,"/",num2,"=", divide(num1,num2)) else: print("Invalid input")
Answered 1 days AfterOct 31, 2021

Answer To: BHI503_Week5_Lab2.pdf BHI503 – eHealth Systems Lab #2: Python programming XXXXXXXXXXPoints Write a...

Shubham Kumar answered on Nov 01 2021
123 Votes
return/ScreenCapture.jpg
return/source.py
def strNcompare(s1, s2, n):
if s1[0:n].lower()==s2[
0:n].lower():
return 0
elif s1[0:n].lower()>s2[0:n].lower():
return -1
return 1
while True:
str1 = input("Enter the first string: ")
str2 = input("Enter the...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here