This is the error we get:
$ python script0.py
File "main.py", line 3
if c[-3:-1i] in ['rmb','RMB','Rmb']:
^
SyntaxError: invalid syntax
Explanation: Looks like a spurious "i" that doesn't belong! Let's remove it.
c = input("enter amount and currency with the sign RMB/EUR:") | |
if c[-3:-1i] in ['rmb','RMB','Rmb']: | |
E=eval(c[0:-3])/7.8942 | |
print("your converted currency is {:.2f}EUR".format(E)) | |
elif c[-3:-1]in['EUR','Eur','eur']: | |
R=eval(c[0:-3])*7.8942 | |
print("your converted currency is {:.2f}RMB".format(R)) | |
else: | |
print("Format Error") |
This is the error we get:
$ python script0.py
File "main.py", line 3
if c[-3:-1i] in ['rmb','RMB','Rmb']:
^
SyntaxError: invalid syntax
Explanation: Looks like a spurious "i" that doesn't belong! Let's remove it.
c = input("enter amount and currency with the sign RMB/EUR:") | |
if c[-3:-1] in ['rmb','RMB','Rmb']: | |
E=eval(c[0:-3])/7.8942 | |
print("your converted currency is {:.2f}EUR".format(E)) | |
elif c[-3:-1]in['EUR','Eur','eur']: | |
R=eval(c[0:-3])*7.8942 | |
print("your converted currency is {:.2f}RMB".format(R)) | |
else: | |
print("Format Error") |
Now, there is no error when we run the program:
$ python script1.py
enter amount and currency with the sign RMB/EUR:341rmb
Format Error
However, the program doesn't recognize our format, even though the input 341rmb
is supposed to be valid. Here's how to debug.
Tip: Determine which codepath your code should've run.
In this case, our first if
condition should've triggered. This means we expect c[-3:-1] in ['rmb','RMB','Rmb']
to be True
. This is the confusing part: our input definitely contains rmb
. So, what is c[-3:-1]
then?
Tip: Print values to check they match your expectations.
Let's print c[-3:-1]
to check it.
c = input("enter amount and currency with the sign RMB/EUR:") | |
print(c[-3:-1]) # added this line for debugging | |
if c[-3:-1] in ['rmb','RMB','Rmb']: | |
E=eval(c[0:-3])/7.8942 | |
print("your converted currency is {:.2f}EUR".format(E)) | |
elif c[-3:-1]in['EUR','Eur','eur']: | |
R=eval(c[0:-3])*7.8942 | |
print("your converted currency is {:.2f}RMB".format(R)) | |
else: | |
print("Format Error") |
Let's run the program again, with the same input.
$ python script2.py
enter amount and currency with the sign RMB/EUR:341rmb
rm
Format Error
Whoa, c[-3:-1]
is rm
? Why doesn't it include the b
? Here is a short series of tests in the interactive prompt to demonstrate why.
>>> test = "helloworld"
>>> test[:-1] # chops off the last letter
'helloworl'
>>> test[-3:] # keeps only the last 3 letters
'rld'
>>> test[-3:-1] # keeps the second-to-last and third-to-last letters
'rl'
Ah, so now we know. We want the last 3 letters, so change this to c[-3:]
. This is our final program.
c = input("enter amount and currency with the sign RMB/EUR:") | |
if c[-3:] in ['rmb','RMB','Rmb']: | |
E=eval(c[0:-3])/7.8942 | |
print("your converted currency is {:.2f}EUR".format(E)) | |
elif c[-3:]in['EUR','Eur','eur']: | |
R=eval(c[0:-3])*7.8942 | |
print("your converted currency is {:.2f}RMB".format(R)) | |
else: | |
print("Format Error") |
Tada! Your code works!
$ python script3.py
enter amount and currency with the sign RMB/EUR:134rmb
your converted currency is 16.97EUR
$ python script4.py
enter amount and currency with the sign RMB/EUR:134eur
your converted currency is 1057.82RMB