## Currency converter variable problems??

This is the place for queries that don't fit in any of the other categories.

### Currency converter variable problems??

Hello, this is the first Python program that I have been given as a task to create, apart from the typical "Hello world" programs at least. So anyway I made my first attempt and got absolutely nowhere, but I sat down, made an algorithm and went on my way... I don't even know if i'm on the right track or anything, but here is the code I have done so far, the program needs to convert to and from 4 currencies, pounds to euro, pounds to yen, pounds to dollars, etc... I figured i'd try pounds to dollars first and got this result in the shell:
Code: Select all
Traceback (most recent call last):
File "C:/Python33/converter.py", line 1, in <module>
pounds = pounds
NameError: name 'pounds' is not defined

My code is below and i'm completely stumped :l I feel like its completely obvious, but I just can't figure it out, if you can give me a few pointers in the right direction or just tell me what i did wrong that'd be great Thanks!
Code: Select all
pounds = pounds
dollars = dollars
yen = yen
euro = euro
print("Welcome to the currency converter")
var1 = input("Please type what currency you wish to convert from ")
var2 = input("Please type the currency that you wish to convert too ")
var3 = int(input("Please type the amount of currency you wish to convert "))
if var1 == pounds and var2 == dollar:
sumusd = var1 * 1.64
print("You will recieve, ", sumusd)
Last edited by stranac on Tue Jan 21, 2014 5:25 pm, edited 1 time in total.
Reason: First post lock.
pythonstudent118

Posts: 5
Joined: Tue Jan 21, 2014 3:45 pm

### Re: Currency converter variable problems??

I'm assuming you wanted to be using strings, e.g.
Code: Select all
pounds = 'pounds'

Although that's probably not needed here, since you can just use literal strings.
Friendship is magic!

R.I.P. Tracy M. You will be missed.

stranac

Posts: 1790
Joined: Thu Feb 07, 2013 3:42 pm

### Re: Currency converter variable problems??

If i put quotes around
Code: Select all
pounds = "pounds"
I just get a ton of syntax errors :s
pythonstudent118

Posts: 5
Joined: Tue Jan 21, 2014 3:45 pm

### Re: Currency converter variable problems??

Can't really help without seeing the traceback...
Friendship is magic!

R.I.P. Tracy M. You will be missed.

stranac

Posts: 1790
Joined: Thu Feb 07, 2013 3:42 pm

### Re: Currency converter variable problems??

Never mind, I was stupid enough to make typing errors :p So I solved that and now i'm getting:
Code: Select all
Traceback (most recent call last):
File "C:/Python33/converter.py", line 9, in <module>
if var1 == pounds and var2 == dollar:
NameError: name 'dollar' is not defined

Code now:
Code: Select all
pounds = 'pounds'
dollars = 'dollars'
yen = 'yen'
euro = 'euro'
print("Welcome to the currency converter")
var1 = input("Please type what currency you wish to convert from ")
var2 = input("Please type the currency that you wish to convert too ")
var3 = int(input("Please type the amount of currency you wish to convert "))
if var1 == pounds and var2 == dollar:
sumusd = var1 * 1.64
print("You will recieve, ", sumusd)
pythonstudent118

Posts: 5
Joined: Tue Jan 21, 2014 3:45 pm

### Re: Currency converter variable problems??

Okay then! Solved that, and now:

Code: Select all
Traceback (most recent call last):
File "C:/Python33/converter.py", line 10, in <module>
sumusd = var3 * 1.64
TypeError: can't multiply sequence by non-int of type 'float'

I'll have a crack at solving this one too then :> do i need to make variable 3 a float some how??

Edit, Done!

Code: Select all
Welcome to the currency converter
Please type what currency you wish to convert from pounds
Please type the currency that you wish to convert too dollars
Please type the amount of currency you wish to convert 1000
You will recieve,  1640.0
>>> ================================ RESTART ================================
>>>
Welcome to the currency converter
Please type what currency you wish to convert from pounds
Please type the currency that you wish to convert too dollars
Please type the amount of currency you wish to convert 500
You will recieve,  820.0
>>> ================================ RESTART ================================
>>>
Welcome to the currency converter
Please type what currency you wish to convert from pounds
Please type the currency that you wish to convert too dollars
Please type the amount of currency you wish to convert 25
You will recieve,  41.0
>>>
Last edited by pythonstudent118 on Tue Jan 21, 2014 6:48 pm, edited 1 time in total.
pythonstudent118

Posts: 5
Joined: Tue Jan 21, 2014 3:45 pm

### Re: Currency converter variable problems??

Was informed that I also need an error message, if the user was to for example time 'dollarsd' it should say something like "Sorry, error occurred, please type a currency"
I've tried a bunch of else and elif statements in different places but none have worked so far, invalid syntaxes
Code: Select all
allowables = ["dollars", "pounds", "euro", "yen"]
pounds = 'pounds'
dollars = 'dollars'
yen = 'yen'
euro = 'euro'
print("Welcome to the currency converter")
var1 = input("Please type what currency you wish to convert from ")
var2 = input("Please type the currency that you wish to convert too ")
var3 = float(input("Please type the amount of currency you wish to convert "))
#[GBP]
if var1 == pounds and var2 == dollars:
sumusd1 = var3 * 1.64
print("You will recieve, ", sumusd)
elif != allowables
print("Sorry, an error has occurred, please input a currency")
print(var1)
if var1 == pounds and var2 == euro:
sumeuro = var3 * 1.22
print("You will recieve, ", sumeuro)
elif != allowables
print("Sorry, an error has occurred, please input a currency")
print(var1)
if var1 == pounds and var2 == yen:
sumyen = var3 * 171.53
print("You will recieve, ", sumyen)
elif != allowables
print("Sorry, an error has occurred, please input a currency")
print(var1)
#[USD]
if var1 == dollars and var2 == pounds:
sumpounds = var3 * 0.61
print("You will recieve, ", sumpounds)
elif != allowables
print("Sorry, an error has occurred, please input a currency")
print(var1)
if var1 == dollars and var2 == euro:
sumeuro = var3 * 0.74
print("You will recieve, ", sumeuro)
elif != allowables
print("Sorry, an error has occurred, please input a currency")
print(var1)
if var1 == dollars and var2 == yen:
sumyen = var3 * 104.16
print("You will recieve, ", sumyen)
elif != allowables
print("Sorry, an error has occurred, please input a currency")
print(var1)
#[EURO]
if var1 == euro and var2 == dollars:
sumusd2 = var3 * 1.64
print("You will recieve, ", sumusd)
elif != allowables
print("Sorry, an error has occurred, please input a currency")
print(var1)
if var1 == euro and var2 == pounds:
sumpounds = var3 * 0.82
print("You will recieve, ", sumpounds)
elif != allowables
print("Sorry, an error has occurred, please input a currency")
print(var1)
if var1 == euro and var2 == yen:
sumyen = var3 * 141.24
print("You will recieve, ", sumyen)
elif != allowables
print("Sorry, an error has occurred, please input a currency")
print(var1)
#[YEN]
if var1 == yen and var2 == dollars:
sumusd3 = var3 * 0.0096
print("You will recieve, ", sumusd3)
elif != allowables
print("Sorry, an error has occurred, please input a currency")
print(var1)
if var1 == yen and var2 == euro:
sumeuro = var3 * 0.0071
print("You will recieve, ", sumeuro)
elif != allowables
print("Sorry, an error has occurred, please input a currency")
print(var1)
if var1 == yen and var2 == pounds:
sumpounds = var3 * 0.0058
print("You will recieve, ", sumpounds)
elif != allowables
print("Sorry, an error has occurred, please input a currency")
print(var1)

Code: Select all
!=

The invalid syntax is on the = sign after the ! on the first elif statement
pythonstudent118

Posts: 5
Joined: Tue Jan 21, 2014 3:45 pm

### Re: Currency converter variable problems??

Code: Select all
elif != allowables

is missing what you are checking is not equal to allowables and missing : from the end
it should look like this
Code: Select all
elif something != somethingelse:

that is what is causing the syntax error

if you meant to do the following
Code: Select all
elif var1 != allowables:

var1 is a single string so will always not equal the list of strings

to check that var1 is not in the list of strings try the following
Code: Select all
elif var1 not in allowables:
Due to the reasons discussed here we will be moving to python-forum.io/ on October 1 2016
This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.

Yoriz

Posts: 1671
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

### Re: Currency converter variable problems??

The way you architect your if/else ideas, it will probably get really messy once you exceed say 5 currencies. You should think about reworking that allocation. Maybe save all exchange rates in a list or dictionary, then dynamically fetch it depending on user input, and main thing: don't write basically the same code over and over again, just changing a number or variable name, etc. For example, you can do like:

Code: Select all
allowables = ["dollars", "pounds", "euro", "yen"]
var1 = input("Please type what currency you wish to convert from ")
while var1 not in allowables:
print("That currency is not known. Choose one of the following:")
print(" or ".join(allowables)]
var1 = input("Please type what currency you wish to convert from ")
# continue here, now var1 is definitely a known currency

Learn: How To Ask Questions The Smart Way
Join the #python-forum IRC channel on irc.freenode.net and chat with uns directly!
Kebap

Posts: 685
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe