Python Forum
re , TypeError: unsupported operand type(s) for -: 'str' and 'str'
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
re , TypeError: unsupported operand type(s) for -: 'str' and 'str'
#1
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')
Reply
#2
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?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Type Error: Unsupported Operand jhancock 2 1,207 Jul-22-2023, 11:33 PM
Last Post: jhancock
  TypeError: unsupported operand type(s) for +: 'dict' and 'int' nick12341234 1 9,335 Jul-15-2022, 04:04 AM
Last Post: ndc85430
  TypeError: unsupported opperand type(s) for %: 'int' and 'list' cool_person 7 2,177 May-07-2022, 08:40 AM
Last Post: ibreeden
  unsupported operand type(s) for %: 'list' and 'int' RandomCoder 4 32,896 May-07-2022, 08:07 AM
Last Post: menator01
  You have any idea, how fix TypeError: unhashable type: 'list' lsepolis123 2 3,016 Jun-02-2021, 07:55 AM
Last Post: supuflounder
  TypeError: __str__ returned non-string (type tuple) Anldra12 1 7,418 Apr-13-2021, 07:50 AM
Last Post: Anldra12
  unsupported operand type(s) for /: 'str' and 'int' Error for boxplot soft 1 3,071 Feb-09-2021, 05:40 PM
Last Post: soft
  TypeError: 'type' object is not subscriptable Stef 1 4,543 Aug-28-2020, 03:01 PM
Last Post: Gribouillis
  TypeError: unhashable type: 'set' Stager 1 2,615 Jun-08-2020, 04:11 PM
Last Post: bowlofred
  TypeError: __repr__ returned non-string (type dict) shockwave 0 3,199 May-17-2020, 05:56 PM
Last Post: shockwave

Forum Jump:

User Panel Messages

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