0

This is my code for a school project (I am writing this in Python 2.7.13):

def euconverter(mon, rate):
    return (mon * rate)

cur = raw_input('Please give me the currency')
mon = raw_input('Please give me the value')
rate = raw_input('Please give me the rate')
while cur == 'EUR' or cur == 'Eur' or cur == 'GBP' or cur == 'Gbp':
    if cur == 'Eur' or cur == 'Eur':
            print (euconverter(mon, rate))
    elif cur == 'GBP' or cur == 'Gbp':
            print (euconverter(mon, rate))
    else:
        if cur != 'EUR' or cur != 'Eur' or cur != 'GBP' or cur != 'Gbp':
            print 'Wrong input'
            break

I get this error:

Traceback (most recent call last):
  File "C:/Users/Maple/PycharmProjects/untitled/Mid term Project.py", line 15, in <module>
    print (euconverter(mon, rate))
  File "C:/Users/Maple/PycharmProjects/untitled/Mid term Project.py", line 2, in euconverter
    return int(mon * rate)
TypeError: can't multiply sequence by non-int of type 'str'

Also, if I type a numeric value when it is asking me for the currency type, then the program exits without displaying any messages. This is a school project, so I am expecting to get wrong inputs from the users and need to provide them with the required error message while trying to make them go back and input the correct one.

Ralf
  • 16,086
  • 4
  • 44
  • 68
  • 1
    `mon` and `rate` are strings. You need to convert them to numbers. – khelwood Nov 09 '18 at 09:02
  • Possible duplicate of [How do I do simple user input in python?](https://stackoverflow.com/questions/4730949/how-do-i-do-simple-user-input-in-python) – khelwood Nov 09 '18 at 09:03
  • Also your `while` is an infinite loop, because you don't read a new value for `cur` inside it. – khelwood Nov 09 '18 at 09:05
  • @khelwood part of the answer is the same, yes but the question also asks about catching input errors – Scott Anderson Nov 09 '18 at 09:06
  • @ScottAnderson Yes, but I can't also vote for "too broad". – khelwood Nov 09 '18 at 09:08
  • and finally, last if should have `and` instead of `or`. Have a look at boolean negation – Michal Polovka Nov 09 '18 at 09:12
  • Okay problem solved with the first error now on the line which asks for the currency type if i type anything else than gbp.GBP etc the program closes with out any input messages and without looping back to where it is asking for the currency type – Maple Sapling Nov 09 '18 at 09:15
  • Possible duplicate of [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – Mr. T Nov 09 '18 at 09:15
  • For the top part of your answer, see what @khelwood linked. But in regards to error catching (technically should be a new question), please consider that your 'while' loop initially tests whether any of the legal possibilities were met ('eur', 'eUr' etc.). If the input was incorrect the python program will never enter the while loop because none of the conditions are true. Because you have no code following the while loop, on an incorrect input it exits without saying anything – Scott Anderson Nov 09 '18 at 09:17
  • Also, a prime example of why there is the rule to ask only one question at a time. – Jongware Nov 09 '18 at 09:59

4 Answers4

1

As mentioned in comments, mon and raw are implicitly strings. To convert them, use e.g. mon = int(mon) or mon = float(mon).

Be careful tho, as you should take care of invalid input (try - except TypeError block).

Few tips: - Last if should use and instead of or - there is no difference between these two lines:

if cur == 'Eur' or cur == 'Eur':
    print (euconverter(mon, rate))
elif cur == 'GBP' or cur == 'Gbp':
    print (euconverter(mon, rate))

you can group them together, if this is intentional

Michal Polovka
  • 628
  • 1
  • 10
  • 21
1

It seems that what you are trying to do is to prompt the user for input and validate it, and then ask again if it is not valid. You can try that by wrapping each raw_input() inside a while-loop with a flag.

To validate the currency it would be easier to cast the input to uppercase and then check agains a list of allowed possibilities.

To validate the amount and rate, you could cast them to float inside a try-except block.

valid_currencies = ['EUR', 'GBP']
cur = None
mon = None
rate = None

is_valid = False
while not is_valid:
    cur = raw_input('Please give me the currency').upper()
    if cur in valid_currencies:
        is_valid = True
    else:
        print 'Not a valid currency'

is_valid = False
while not is_valid:
    try:
        mon = float(raw_input('Please give me the value'))
        is_valid = True
    except ValueError:
        print 'Not a valid value'

is_valid = False
while not is_valid:
    try:
        rate = float(raw_input('Please give me the rate'))
        is_valid = True
    except ValueError:
        print 'Not a valid rate'

print 'Converted amount'
print mon * rate
Ralf
  • 16,086
  • 4
  • 44
  • 68
0

Some refactored code for your text input would look like:

legal_input = ["gbp", "eur", "yen"]
currency = raw_input("Please enter the currency type")
if currency.lower() in legal_input:
    #some code to do the required operations
else:
    print('not a recognised currency type!')
Scott Anderson
  • 631
  • 5
  • 26
0
def euconverter(mon, rate):
    return mon * rate


cur = raw_input('Please give me the currency type ')
mon = float(raw_input('Please give me the ammount of money '))
rate = float(raw_input('Please give me the rate of the exchange '))
while True:
    #i dont know how to make the false statement ending the program as well not necessary i think
    if cur == 'EUR' or cur == 'Eur' or cur == 'GBP' or cur == 'Gbp':
        if cur == 'EUR' or cur == 'Eur':
            print (euconverter(mon, rate))
            break
        elif cur == 'GBP' or cur == 'Gbp':
            print (euconverter(mon, rate))
            break
            # I dont know if multiple breaks should be included here
    else:
        print 'wrong input'
        cur = raw_input('Please give me the currency type again correcntly this time ')
        continue

With tips and help from everyone i finally got it running prompting the user to enter an other currency in the currency input line when wrong appreciate your help you'll be hearing from me soon again xD