Currency converter variable problems??

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

Currency converter variable problems??

Postby pythonstudent118 » Tue Jan 21, 2014 4:01 pm

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 :D 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??

Postby stranac » Tue Jan 21, 2014 5:35 pm

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.
User avatar
stranac
 
Posts: 1143
Joined: Thu Feb 07, 2013 3:42 pm

Re: Currency converter variable problems??

Postby pythonstudent118 » Tue Jan 21, 2014 6:22 pm

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??

Postby stranac » Tue Jan 21, 2014 6:23 pm

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

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1143
Joined: Thu Feb 07, 2013 3:42 pm

Re: Currency converter variable problems??

Postby pythonstudent118 » Tue Jan 21, 2014 6:31 pm

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??

Postby pythonstudent118 » Tue Jan 21, 2014 6:44 pm

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! :D

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??

Postby pythonstudent118 » Tue Jan 21, 2014 9:58 pm

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??

Postby Yoriz » Tue Jan 21, 2014 10:28 pm

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:
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 827
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Currency converter variable problems??

Postby Kebap » Wed Jan 22, 2014 11:04 am

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: 396
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 3 guests