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.
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')
