mark tennant mark tennant - 4 months ago 8
Python Question

Python 3.4 code help - I can't make this work

I am new to python. This is my code:

print("Welcome to the Currency Converter")

print("This Currency Converter converts: ")

print(" US [D]ollar")

print(" [E]uro")

print(" British[P]ound Sterling")

print(" Japanese[Y]en")

print()

def input1() :

a = input("Enter the currency inital {eg. [E] for Euro} you wish to convert from: ")
if a.lower is not ('e','y','p','d'):
print("That is not a valid currency inital. Please try again ")
input1()
input1()

def input2() :

b = input("Enter the currency inital you wish to convert to: ")
if b.lower is not ('e','y','p','d'):
print("That is not a valid currency inital. Please try again")
input2()
input2()


It's always repeating even though I don't want it to. My aim is to make it work only if
e
,
y
,
d
or
p
is entered, otherwise it should display the error message and repeat the question.

Answer

Your code doesn't properly call the functions nor does it assign global variables for conversion. In addition, you shouldn't use the is keyword, which checks for equivalent references in memory, you should use the in keyword which checks for the existence of an element in your tuple.

print("Welcome to the Currency Converter")

print("This Currency Converter converts: ")

print(" US [D]ollar")

print(" [E]uro")

print(" British[P]ound Sterling")

print(" Japanese[Y]en")

print()

def input1() :

    a = input("Enter the currency inital {eg. [E] for Euro} you wish to convert from: ")
    if a.lower() not in ('e','y','p','d'):
        print("That is not a valid currency inital. Please try again ")
        return input1()
    return a

a = input1()

def input2() :

    b = input("Enter the currency inital you wish to convert to: ")
    if b.lower() not in ('e','y','p','d'):
        print("That is not a valid currency inital. Please try again")
        return input2()
    return b

b = input2()
Comments