Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

please help me with this assigment. python! 7.11 LAB: Make a number The float function in Python maybe a little too strict in the rules

please help me with this assigment. python! image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
7.11 LAB: Make a number The float function in Python maybe a little too strict in the rules to convert a string to a float. Consider web pages where a user can enter a value into a form We may want our form to allow ently of numbers where the rules are more relaxed. We would like to enable a user to enter a numeric number (a sequence of digits) into the form allowing 1) Leading or trailing white space 2) Embedded single space (but not two spaces in a row) 3) Optional or but if used must be the first non white space character indicates a negative number) 4) Enclosed in parentheses (also indicates a negative number) 5) An optional decimal point (but only one decimal point more than one would be invalid) 6) The optional eritey of commas (but only every third position to the left of the decimal point or end of the string if no decirnal point is given, and must have at least one digit before a comma) Any other character than digits or those listed as valid above, is an error Write a function parserum which will accept a string and retum a float provided the string conforms to the specification. If the string does not abide by the specified options then returns an invalid number' message as shown below String "1995" 20 "3.99" "(.095) "2.999.888 -1.000.01" "6, 001. 02" 1 2 3 (12) + 67 11 Value Returned 1995.0 20.0 3.99 -0.095 2999888 -1000.01 6001.02 123.0 -12.0 12.0 67.0 MInvalid number - embedded sequence of more than one space" "Invalid number - no value provided" "Invalid number - no value provided" Invalid number - no value provided "Invalid number - more that one decimal point "Invalid number - misplaced or " "Invalid number - missing for negative number" "Invalid number - misplaced cloning parenthesis" "Invalid number - misplaced opening parenthesis "Invalid number - misplaced com "Invalid number - misplaced leading comma "Invalid number - misplaced comma "Invalid number - misplaced comma" "Invalid number - misplaced comma "Invalid number - misplaced comma after decimal point" "Invalid number - encountered A" 4.5.2 1452 + (1 2 (1) 2 "34,12 , 128.06 "23, 345, 12.34 "2345, 120" "2,3456, 1201 "345.345, 560 "344" To test write a program that inputs a string and print the result of parse_num when passed the string Suggested strategy approach this problem incrementally. That is writing a small section of code and run the tests. Keep writing small sections of code and run the tests after coding each section, parse_num will ultimately return calling the float function on the input string This should be the first section of code implemented and tested, Then 1) Add code to strip the leading and trailing whitespace (while the float function will handle this, doing so will prepare the input string for further edits and checking error conditions 2) Add code to check if the input string is empty. If so return appropriate invalid number message 3) See if input string begins with a If so set a variable neg to-1. If not set neg to 1. In the return statement multiply return of the float function by neg parsenum is going to handle the sign as parsenum has to handle further checks 4) Check if the input string begins with or 't remove this sign from the beginning of inputsting. Recheck if the inputstring is now empty an if so return appropriate invalid number message: 5) See if the input string begins with (if so check if inputstring ends with and if not return appropriate invalid number message. Even if there is a closing )check if there is an embedded ' and if so return appropriate invalid number message. If there is a closing then multiply neg variable by 1 and remove the leading and trailing parentheses from the input string Recheck if the inputstring is now empty an if so return appropriate invalid number message 6) Check if the input string has any + or signl remaining. If so return appropriate invalid number message 7) Check if the input string has more than one decimal point. It so return appropriate invalid number message, 8) Check if the input string has a sequence of two spaces. If so return appropriate invalid number message 9) Remove all spaces 10) Now check for appropriate use of commas, decimal point, and that the only other characters are digits. This may involve a loop keeping track of the index position of each character Before loop set variables commapos and deposto 1. This is to indicate that commas and decimal points have not yet been encountered in the loop. Set comma position to index position. If a non-digit, comma, or decimal point in encountered, return appropriate invalid number message, 10) Now check for appropriate use of commas decimal point and that the only other characters are digits. This may involve a loop keeping track of the index position of each character. Before loop set variables commapos and apposto -1. This is to indicate that commas and decimal points have not yet been encountered in the loop Set comme position to index position. If a non-digit comma ordecimal point in encountered, retum appropriate invalid number message 1 Check for and if so return appropriate invalid number message 2. If a comma is encountered after a decimal point has been encountered return appropriate invalid number message 3. If the first timea comma is encountered, check Index greater than 1 but less than 4. If so set comma position If not return appropriate Invalid number message 4. If a commis encountered check Index position is more than test comma position. It so set comma position if not return appropriate invalid number message 5. It a decimal point is encountered also check index position is 4 more than last comme position if so set decimal point position. If not return appropriate invalid number message 6. If a non-digit corruma, or decimal point is encountered return appropriate invalid number message 11) After the loop of 10, check the length of the string is four more that the index of the last comma (if encountered I not retum appropriate valid number message 1) Remove al commas Only show failing tests Download this s 1: Compare output Input 1995 Your output your program produced no output Expected output 1995.0 2. Compare output Input 20 Your output your program produced no output Expected output 20.0 3. Compare output Input 3.99 Your output your program produced no output Expected output 3.99 4 Compare output Input (.095) Your output your program produced no output Expected output -0.095

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Visual C# And Databases

Authors: Philip Conrod, Lou Tylee

16th Edition

1951077083, 978-1951077082

More Books

Students also viewed these Databases questions

Question

Explain the nature of human resource management.

Answered: 1 week ago

Question

Write a note on Quality circles.

Answered: 1 week ago

Question

Describe how to measure the quality of work life.

Answered: 1 week ago

Question

What is the basis for Security Concerns in Cloud Computing?

Answered: 1 week ago

Question

Describe the three main Cloud Computing Environments.

Answered: 1 week ago