Python Forum
Error in Python script
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Error in Python script
#8
(May-24-2020, 05:15 PM)garvind25 Wrote: Thanks for your reply. Yes, I am expected to account for invalid inputs. How to do so pls.?

Regards,
Arvind Gupta


If your instructor is expecting you to account for invalid inputs, I presume you have been taught a method for doing so. I can't do your homework for you, but here is a generic approach using try/except:
my_string = input() # remember, any value retrieved from input() is a string by default

try: # use try when you will attempt to do something that may result in an error

    my_string_float = float(my_string)

except ValueError: # this line determines what happens if an error of type 'ValueError' occurs

    print('Could not convert my_string to float.')
You will probably want to put the input() and try/except all into a loop, so that you can get new input from the user if the error occurs. If this doesn't give you enough information to complete your assignment, please post the full details of the assignment so that we can help you further.
Reply


Messages In This Thread
Error in Python script - by garvind25 - May-23-2020, 07:40 PM
RE: Error in Python script - by buran - May-23-2020, 07:53 PM
RE: Error in Python script - by garvind25 - May-23-2020, 09:31 PM
RE: Error in Python script - by GOTO10 - May-23-2020, 10:56 PM
RE: Error in Python script - by garvind25 - May-24-2020, 05:15 PM
RE: Error in Python script - by pyzyx3qwerty - May-24-2020, 05:34 PM
RE: Error in Python script - by buran - May-24-2020, 05:47 PM
RE: Error in Python script - by GOTO10 - May-24-2020, 09:04 PM
RE: Error in Python script - by garvind25 - May-25-2020, 06:02 PM
RE: Error in Python script - by buran - May-25-2020, 06:34 PM
RE: Error in Python script - by GOTO10 - May-25-2020, 08:50 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Re-write BASH script to Python script popi75 5 2,468 Apr-30-2021, 03:52 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020