IF statement picking first option in currency converter (python)
<pre><code>print "Pound Sterling converter"
print "You can convert pounds to either dollars, euros or yen"

print

dollar = 0
euro = 0
yen = 0

convert_to = input ("What currency do you want to convert to? ")
amount = input("How much would you like to convert? ")

print

if convert_to == dollar:
    amount = amount * 1.3
elif convert_to == euro:
     amount = amount * 1.17
elif convert_to == yen:
    amount = amount * 133.66
else:
    print "You must pick either dollar, euro or yen."

print amount
</code></pre>

<p>I'm a beginner in Python, as you can probably tell. All I want this program to do is have the user choose a currency (convert_to) and then choose how much they want to convert (amount) and then the program will convert it for them.</p>

<p>When I run the program, the if statement does not work correctly. Instead of seeing what convert_to is, it goes through the convert_to == dollar part regardless of if you type euro or yen. The numbers they are being multiplied by are simply the conversion rates from pounds.</p>

<p>also, a side note but less important one, the final else part does not work. The program brings up a "input not defined" error instead of printing "You must pick either dollar, euro or yen."</p>

<p>Thanks in advance</p>

<p>If i understand correctly, the user will input either "0", "1" or "2" as a input, for the convertion for dollar, euro or yen respectively. So you need to change the initial values of dollar, euro and yen. Changing that, the version with integer input will work.</p>

<pre><code>dollar = 0
euro = 1
yen = 2
</code></pre>

<p>If the input is a string such as "dollar", "euro" or "yen", the variables need to be changed to these respective strings, so firstly you need to set the variable as:</p>

<pre><code>dollar = "dollar"
euro = "euro"
yen = "yen"
</code></pre>

<p>Then change the input type for raw_input, as python requires this type of input to recognize a String input. So change <code>convert_to</code> to:</p>

<pre><code>convert_to = raw_input ("What currency do you want to convert to? ")
</code></pre>

<p>If you try to pass a string to a <code>input</code> variable it will always return 0. And since the initial values for all the variable are 0, the if statements returns true when comparing <code>convert_to</code> to <code>dollar</code> , <code>euro</code> or <code>yen</code>. Because the comparsion to the dollar is hapening first, it always goes to that case.
These are the changes necessary for the code to run.</p>

<p>Extra: You also don't need the variables in this case, since you're only using them in one field. So on your if statements, you can just compare them to the specific strings, as:</p>

<pre><code>if convert_to == "dollars":
    amount *= 1.3
elif convert_to == "euros":
     amount *=  1.17
elif convert_to == "yen":
    amount *= 133.66
else:
    print "You must pick either dollar, euro or yen." 
</code></pre>

<p>Hope it helps.</p>

python if-statement