Jump to content

Printing an error message

Go to solution Solved by Guest,
# previous reading from user
previousRead = input (str ("Please enter previous Gas Meter Reading: "))
previousReadLen = len (previousRead) # length of input data
if previousReadLen < 4 :
    previousRead = int (previousRead) # switching from string to integer
    print ("ERROR IN THE PREVIOUS READING (USE FOUR DIGIT NUMBER(%.4d)). TRY AGAIN!" %previousRead)
previousRead = int (previousRead) # switching from string to integer
if previousRead < 0000 or previousRead > 9999 :
    print ("ERROR IN PREVIOUS READING (OUT OF RANGE:0000-9999). TRY AGAIN!")
   
# current reading from user
currentRead = input (str ("Please enter new Gas Meter Reading: "))
currentReadLen = len (currentRead) # length of input data
if currentReadLen < 4 :
    currentRead = int (currentRead) # switching from string to integer
    print ("ERROR IN THE NEW READING (USE FOUR DIGIT NUMBER(%.4d)). TRY AGAIN!" %currentRead)
currentRead = int (currentRead) # switching from string to integer
if currentRead < 0000 or currentRead > 9999 :
    print ("ERROR IN NEW READING (OUT OF RANGE:0000-9999). TRY AGAIN!")
elif currentRead < previousRead :
    currentRead = currentRead + 10000

Thanks @M.Yurizaki for you help! <3

"""
### Program Design ###
Purpose: calculate the monthly bill based on the gas consumption
Input data: gas consumption
Output data: montly gas bill
Algorithm:
    - create header
      print "*" * 70
      print title "*", " " * 10, "Welcome to Gas Bill Calculator", " " * 15, "*"
      print title "*", "(This program developed by student name (ID: ******))" "*"
      print "*" * 70
    - customer name from user
    - previous month reading from user
    - current meter reading from user
    - compute the amount of gas comsumed
      gasCons = abs (currentRead - previousRead)
    - compute the bill
      gasBill = gasCons * appliedRate
    - print the result
"""
### Program Implementation ###

# create header
print ("*" * 60)
print ("*", " " * 9, "Welcome to Gas Bill Calculator", " " * 15, "*")
print ("*", "(This program developed by student name (ID: ******) " "*")
print ("*" * 60)

# customer name from user
custName = input ("Please enter your name: ")

# previous month reading from user
previousRead = int (input ("Please enter previous Gas Meter Reading: "))
if previousRead < 0000 or previousRead > 9999 :
    print ("ERROR IN PREVIOUS READING(OUT OF RANGE:0000-9999). TRY AGAIN!")
elif previousRead != int (    ) :
    print ("ERROR IN THE PREVIOUS READING(USE FOUR DIGIT NUMBER(%.4d)). TRY AGAIN!" %previousRead)

# current meter reading from user    
currentRead = int (input ("Please enter new Gas Meter Reading: "))
if currentRead < 0000 or currentRead > 9999 :
    print ("ERROR IN NEW READING(OUT OF RANGE:0000-9999). TRY AGAIN!")
elif currentRead != int (    ) :
    print ("ERROR IN THE NEW READING(USE FOUR DIGIT NUMBER(%.4d)). TRY AGAIN!" %currentRead)

# compute the amount of gas consumed
gasCons = abs (currentRead - previousRead)
    
# compute the bill
minimumCost = 3.500
if gasCons >= 0 or gasCons <= 70 :
    gasBill = minimumCost
elif gasCons >= 71 or gasCons <= 170 :
    gasBill = minimumCost + 0.0003 / gasCons
elif gasCons >= 171 or gasCons <= 400 :
    gasBill = minimumCost + 0.0015 / gasCons
else :
    gasBill = minimumCost + 0.0075 / gasCons

# print the result
print ("Dear ", custName, ", your gas comsumption bill is as follows:")
print ("-" * 60)
print ("Gas Consumed:", " " * 21, "%.d cubic meters" %gasCons)
print ("Bill amount:", " " * 20, "%.3f Omani Rials" %gasBill)
print ("-" * 60)

 

I'm trying to create a program where you have to use 4 digits for it to continue and if you don't then you get an error message. It works well when i use 1 - 3 digits but i still get an error when I use the intended 4 digits. (previous and current readings)

I also want to know how to make the program restart whenever it shows the error message, currently it will prints the error message but would still print the next input message.

 

Anyone knows how to make this work? Any tips?

 

*** You can ignore the computing part of this program, it's still needs tuning. ***

Link to comment
https://linustechtips.com/topic/901371-printing-an-error-message/
Share on other sites

Link to post
Share on other sites

3 hours ago, lewdicrous said:

 

I'm trying to create a program where you have to use 4 digits for it to continue and if you don't then you get an error message. It works well when i use 1 - 3 digits but i still get an error when I use the intended 4 digits. (previous and current readings)

I also want to know how to make the program restart whenever it shows the error message, currently it will prints the error message but would still print the next input message.

 

Anyone knows how to make this work? Any tips?

 

*** You can ignore the computing part of this program, it's still needs tuning. ***

elif previousRead != int (    ):
elif currentRead != int (    ) :

does not check if the number is not an int. You want to use 

if not isinstance(currentRead, int) : 

 

Link to post
Share on other sites

1 hour ago, elpiop said:

does not check if the number is not an int. You want to use 

if not isinstance(currentRead, int) :

How can I make it to where it allows 4 digit values (i.e 1000) but gives me an error if it was something like 324 or 12?

 

I already set up the appropriate message for such error but i still don't know how to set that 'digit limit'

print ("ERROR IN THE PREVIOUS READING(USE FOUR DIGIT NUMBER(%.4d)). TRY AGAIN!" %previousRead)

print ("ERROR IN THE NEW READING(USE FOUR DIGIT NUMBER(%.4d)). TRY AGAIN!" %currentRead)

 

 

Thank you!

Link to post
Share on other sites

If you are enforcing 4 digits by making the user input leading zeros, I'd call that poor UX design. You shouldn't be forcing people to enter in values that don't have any actual meaning unless there's some technical reason why they must.

Link to post
Share on other sites

2 minutes ago, M.Yurizaki said:

If you are enforcing 4 digits by making the user input leading zeros, I'd call that poor UX design. You shouldn't be forcing people to enter in values that don't have any actual meaning unless there's some technical reason why they must.

It's part of the question that I'm trying to solve, if it were up to me then I wouldn't enforce such a rule.

Here are some hints that are found in the question:

5a982f2a9ae65_Screenshot-2018-3-1SultanQaboosUniversity-COMP2101_HW2_SP18pdf(1).png.5abfaaef74ce9ece31befacab2c0d8e1.png

Here are sample runs provided: (I'm trying to produce something like that)

5a982f2c3d10f_Screenshot-2018-3-1SultanQaboosUniversity-COMP2101_HW2_SP18pdf(2).png.575adb08bd1aaa8c561c82127026118c.png

Link to post
Share on other sites

5 minutes ago, lewdicrous said:

It's part of the question that I'm trying to solve, if it were up to me then I wouldn't enforce such a rule.

Here are some hints that are found in the question:

 

Here are sample runs provided: (I'm trying to produce something like that)

 

People wonder why bad programming propagates :P

 

Anyway, use raw_input. This will give you the string version of what was inputted. Then you can enforce the input length by using len(input_var).

Link to post
Share on other sites

1 minute ago, M.Yurizaki said:

People wonder why bad programming propagates :P

Out of my hand unfortunately lol

1 minute ago, M.Yurizaki said:

Anyway, use raw_input. This will give you the string version of what was inputted. Then you can enforce the input length by using len(input_var).

I'll try it out, thanks!

Link to post
Share on other sites

1 minute ago, lewdicrous said:

Out of my hand unfortunately lol

I'll try it out, thanks!

As a side note, raw_input takes in a string as an argument to display a prompt. So you can go raw_input('Enter new meter value: ') and it'll print that out. Make sure to put a trailing space at the end (unless you don't mind "Enter new meter value:1234")

Link to post
Share on other sites

2 minutes ago, M.Yurizaki said:

As a side note, raw_input takes in a string as an argument to display a prompt. So you can go raw_input('Enter new meter value: ') and it'll print that out. Make sure to put a trailing space at the end (unless you don't mind "Enter new meter value:1234")

I'm trying to use that but it keeps on giving me syntax errors, read somewhere that it they changed it for python 3. Is that true or am I just being an idiot?

Link to post
Share on other sites

4 minutes ago, lewdicrous said:

I'm trying to use that but it keeps on giving me syntax errors, read somewhere that it they changed it for python 3. Is that true or am I just being an idiot?

Oh, if you're using Python 3, raw_input() changed to input(). What I said earlier still applies though.

Link to post
Share on other sites

# previous reading from user
previousRead = input (str ("Please enter previous Gas Meter Reading: "))
previousReadLen = len (previousRead) # length of input data
if previousReadLen < 4 :
    previousRead = int (previousRead) # switching from string to integer
    print ("ERROR IN THE PREVIOUS READING (USE FOUR DIGIT NUMBER(%.4d)). TRY AGAIN!" %previousRead)
previousRead = int (previousRead) # switching from string to integer
if previousRead < 0000 or previousRead > 9999 :
    print ("ERROR IN PREVIOUS READING (OUT OF RANGE:0000-9999). TRY AGAIN!")
   
# current reading from user
currentRead = input (str ("Please enter new Gas Meter Reading: "))
currentReadLen = len (currentRead) # length of input data
if currentReadLen < 4 :
    currentRead = int (currentRead) # switching from string to integer
    print ("ERROR IN THE NEW READING (USE FOUR DIGIT NUMBER(%.4d)). TRY AGAIN!" %currentRead)
currentRead = int (currentRead) # switching from string to integer
if currentRead < 0000 or currentRead > 9999 :
    print ("ERROR IN NEW READING (OUT OF RANGE:0000-9999). TRY AGAIN!")
elif currentRead < previousRead :
    currentRead = currentRead + 10000

Thanks @M.Yurizaki for you help! <3

Link to post
Share on other sites

12 minutes ago, lewdicrous said:

inputVal = input ("Please enter previous Gas Meter Reading: ")
previousRead = str (inputVal)
previousRead = len(previousRead)

if previousRead < 4 :
    previousRead = int (inputVal)
    print ("ERROR IN THE PREVIOUS READING (USE FOUR DIGIT NUMBER(%.4d)). TRY AGAIN!" %previousRead)


inputVal = input ("Please enter new Gas Meter Reading: ")
currentRead = str (inputVal)
currentRead = len(currentRead)
if currentRead < 4 :
    currentRead = int (inputVal)
    print ("ERROR IN THE NEW READING (USE FOUR DIGIT NUMBER(%.4d)). TRY AGAIN!" %currentRead)

Thanks @M.Yurizaki for you help! <3

If you want to make it even more robust to input errors, wrap the "int (inputVal)" with a try/except block. Otherwise if someone enters letters the int() function will throw an exception and basically "crash" the script.

 

So it'd look something like:

try:
  currentRead = int(inputVal)
  # do something
except:
  print('Number not inputted')
  

But since you're only expecting integers, you could also check if the string is a number by using .isdigit() on the string itself. e.g.,:

if currentRead < 4:
  if currentRead.isdigit() == False:
    print ('The input is not a number')
  else:
    # Do something

 

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×