0

I'm trying to make a very simple Python program that will convert USD to several different currencies. I am having trouble with an IF/ELIF statement. Whenever I run the code in my interpreter, in this case Pycharm, the interpreter will prompt me to input the value I want to convert, followed by the currency I want to convert to, and then finish without printing anything that I have set up in the IF/ELIF statement.

Code:

import os

os.system('cls')

# This code starts the program and explains
# the purpose of the program to the user.
print()
print("Hello User")
print("This program will convert dollars to yen.")
print()

# This code prompts the user to enter a dollar amount.
USD = input("Amount of dollars: ")

# This code converts the user entered amount as a floating
# point integer for ease of calculation and error correcting.
var1 = float(USD)

print("YEN: 1")
print("EUR: 2")
print("RUB: 3")
print("Yuan: 4")
var2 = input("What Currency Do You Want To Convert To?: ")


def my_converter():
    if var2 == 1:
        var3 = var1 * float(114.62)
        var4 = "Yen"
        print(USD, "U.S. Dollars equals", var3, var4)
    elif var2 == 2:
        var3 = var1 * float(1.14)
        var4 = "Euro"
        print(USD, "U.S. Dollars equals", var3, var4)
    elif var2 == 3:
        var3 = var1 * float(0.015)
        var4 = "Rubles"
        print(USD, "U.S. Dollars equals", var3, var4)
    elif var2 == 4:
        var3 = var1 * float(0.15)
        var4 = "Yuan"
        print(USD, "U.S. Dollars equals", var3, var4)

I am just getting into Python so I'm probably just missing something really obvious, but any help would be greatly appreciated. Apologies if this is a duplicate or dumb question.

Dev-MDW2
  • 113
  • 9
  • 2
    try casting your var2 to an int. Also your converter code is never run so you need to call my_converter() – nathan.medz Jan 16 '19 at 23:58
  • 2
    This program does not do any conversions. You need to call `my_converter()` somewhere. I strongly suggest to follow a tutorial for programming with Python. – miraculixx Jan 17 '19 at 00:00
  • 1
    First, look at what nathan said above. However, you are defining a method, then not calling it. It is also not proper to use global variables like this, and you should pass them to the my_converter function. (Also, since you print the same thing at the end, you can put it outside of any elif block) – robert Jan 17 '19 at 00:01
  • Thank for the info, I wasn't aware that I have to cast an input as an int when the input is going to be an integer, though looking at that statement now, that does make logical sense. I am used to the Linux bash shell and VBA, where I don't necessarily have to do so. – Dev-MDW2 Jan 17 '19 at 00:14

3 Answers3

1

var2 type is string, convert it to int.

var2 = int(var2)

As you know, '1' == 1 is False.

1

You did not call the function my_converter(). To call a function essentially means to run it. To call the function, just type my_converter() on a line that's not indented, at the end of the program.

1

Two small issues, you are not calling the function and you are trying to compare a string, from the input, and an int, in your logical test.

...
var2 = int(input("What Currency Do You Want To Convert To?: "))

def my_converter():
    if var2 == 1:
        var3 = var1 * float(114.62)
        var4 = "Yen"
        print(USD, "U.S. Dollars equals", var3, var4)
    elif var2 == 2:
        var3 = var1 * float(1.14)
        var4 = "Euro"
        print(USD, "U.S. Dollars equals", var3, var4)
    ...


my_converter()

This now works as expected

tmcnicol
  • 576
  • 3
  • 14
  • 1
    This solved it. I forgot to call it, and I was unaware that I have to use an "int(" to get an integer input. Thank you very much @tmcnicol – Dev-MDW2 Jan 17 '19 at 00:11