Python Forum
re , TypeError: unsupported operand type(s) for -: 'str' and 'str' - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: re , TypeError: unsupported operand type(s) for -: 'str' and 'str' (/thread-8698.html)



re , TypeError: unsupported operand type(s) for -: 'str' and 'str' - steponesmall - Mar-04-2018

Hello

I scripted a car oil change app. it is working fine but i want to add condition that input must be numeric. I tried using 're'. But with my current code if i use re expressions my code breaks and give error. so i removed int from input. now it can validate if user is using numeric but after that it gives error.

TypeError: unsupported operand type(s) for -: 'str' and 'str'

Thanks in advance
Please suggest me how can i fix this.
#######################
#code without re
#######################
#display Welocme message in start

print('-'*40)
print('Welcome to Oil Change app.\nMiles Between Oil Change is 7500')
print('-'*40)
print('')

#set varibles for oil change
miles_between_oil_change = 7500
valid_entries = False


while not valid_entries:
    print('')
    #get user input
    last_oil_change = int (input('When did last oil change: '))

    current_miles = int (input('What is current milage: '))
    #current milage must be higer then last oil change
    if (current_miles<last_oil_change):
     print('')
     print('Invalid Info! Current Milage must be higher then last oil change.\n')
     print('#'*65)
    else:
        #miles travel since last oil change
        miles_travel = current_miles - last_oil_change
        valid_entries = True
        print('-'*30)
        print('Since oil change you have run: ',miles_travel,'KMS')
        print('')

#caluculation code here
run = (current_miles-last_oil_change)
next_oil_change = (last_oil_change + miles_between_oil_change)
due_in = (next_oil_change-current_miles)
over_run = (run-miles_between_oil_change)


print   ('Next Oil Change: ',next_oil_change,'KMS.')
print('-'*30)

print('')
if (next_oil_change>run):
    print ('',due_in,'KMS are left for next oil change.')
    print('')
elif (run-miles_between_oil_change):
    print ('Warning! Over run :',over_run,'KMS,\nChange oil.')
    print('')

print('-'*30)
print('Program Ends, Have A Nice Day\n')
##############################################
#With re expression and removed int from intput 
#it does validate userinfo  but break afterwords.
###############################################

import re
#display welocme message in start

print('-'*40)
print('Welcome to Oil Change app.\nMiles Between Oil Change is 7500')
print('-'*40)
print('')

#set varibles for oil change
miles_between_oil_change = 7500
valid_entries = False


while not valid_entries:
    print('')
    #get user input
    last_oil_change = input('When did last oil change: ')
    miles = re.compile(r'[0-9]+')
    current_miles = input('What is current milage: ')
    #validate userinfo
    while not miles.match(current_miles):
      print ("invalid characters, Use numeric ")
      current_miles = input('What is current milage: ')

    #current milage must be higer then last oil change
    if (current_miles<last_oil_change):
     print('')
     print('Invalid Info! Current Milage must be higher then last oil change.\n')
     print('#'*65)
    else:
        #miles travel since last oil change
        miles_travel = current_miles - last_oil_change
        valid_entries = True
        print('-'*30)
        print('Since oil change you have run: ',miles_travel,'KMS')
        print('')

#caluculation code here
run = (current_miles-last_oil_change)
next_oil_change = (last_oil_change + miles_between_oil_change)
due_in = (next_oil_change-current_miles)
over_run = (run-miles_between_oil_change)


print   ('Next Oil Change: ',next_oil_change,'KMS.')
print('-'*30)

print('')
if (next_oil_change>run):
    print ('',due_in,'KMS are left for next oil change.')
    print('')
elif (run-miles_between_oil_change):
    print ('Warning! Over run :',over_run,'KMS,\nChange oil.')
    print('')

print('-'*30)
print('Program Ends, Have A Nice Day\n')



RE: re , TypeError: unsupported operand type(s) for -: 'str' and 'str' - mpd - Mar-04-2018

A TypeError is a Python exception. In this case, it's "thrown" or "raised" when the argument to int() is not a number. Exceptions are a common way for raising errors. Check out the tutorial about them here: https://docs.python.org/3/tutorial/errors.html

In general, please format your code using BBCode tags: https://python-forum.io/misc.php?action=help

Also, don't just say "it breaks." What breaks? What should it be doing? What is it doing instead? What, if any, error messages are you getting?