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.

10
  • 1
    mon and rate are strings. You need to convert them to numbers.
    – khelwood
    Nov 9, 2018 at 9:02
  • Possible duplicate of How do I do simple user input in python?
    – khelwood
    Nov 9, 2018 at 9:03
  • Also your while is an infinite loop, because you don't read a new value for cur inside it.
    – khelwood
    Nov 9, 2018 at 9:05
  • @khelwood part of the answer is the same, yes but the question also asks about catching input errors Nov 9, 2018 at 9:06
  • @ScottAnderson Yes, but I can't also vote for "too broad".
    – khelwood
    Nov 9, 2018 at 9:08

4 Answers 4

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

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
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!')
2
  • We aren't taught how to use legal input therefore i cannot add it to a project :/ Nov 9, 2018 at 9:28
  • Ok well read my comment on the question as to why your while loop stops you from catching any input errors Nov 9, 2018 at 10:02
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.