简体   繁体   中英

Python currency converter, float object is not iterable

currency_input = input('Enter GBP values (separated by whitespace): ')
currency_list = currency_input.split()

# arrays to store currencies and converted currencies
GBPvalues = []
convertedEuros = []
convertedDollars = []
convertedYen = []
convertedRupee = []

# stores GBP values in array GBPvalues
for x in currency_list:
    GBPvalues.append(int(x))

# function for converting GBP to euros
def euroConversion():
    for x in GBPvalues:
        euros = x * 114 / 100
        for e in euros:
            convertedEuros.append(euros)

# function for converting GBP to dollars
def dollarConversion():
    for x in GBPvalues:
        dollars = x * 134 / 100
        for d in dollars:
            convertedDollars.append(float(dollars))

# functions for converting GBP to yen
def yenConversion():
    for x in GBPvalues:
        yen = x * 15074 / 100
        for y in yen:
            convertedYen.append(float(dollars))

# functions for converting GBP to rupee
def rupeeConversion():
    for x in GBPvalues:
        rupee = x * 8614 / 100
        for r in rupee:
            convertedRupee.append(float(rupee))

# function to output the currency and converted currency
def conversionOutput():
    print('1. Euros')
    print('2. Dollars')
    print('3. Yen')
    print('4. Rupees')
    print('5. Exit')

    userInput = input()
    if userInput == '1':
        euroConversion()

        for i in range(10):
            print(GBPvalues[i] + convertedEuros[i])

        # adds up total of GBP and euros
        totalGBP = sum(GBPvalues)
        totalEuros = sum(convertedEuros)
        print(totalGBP)
        print(totalEuros)

    if userInput == '2':
        dollarConversion()

        for i in range(10):
            print(GBPvalues[i] + convertedDollars[i])

        # adds up total of GBP and dollars
        totalGBP = sum(GBPvalues)
        totalDollars = sum(convertedDollars)
        print(totalGBP)
        print(totalDollars)

    if userInput == '3':
        yenConversion()

        for i in range(10):
            print(GBPvalues[i] + convertedYen[i])

        # adds up total of GBP and yen
        totalGBP = sum(GBPvalues)
        totalYen = sum(convertedYen)
        print(totalGBP)
        print(totalYen)

    if userInput == '4':
        rupeeConversion()

        for i in range(10):
            print(GBPvalues[i] + convertedRupee[i])

        # adds up total of GBP rupee
        totalGBP = sum(GBPvalues)
        totalRupee = sum(convertedRupee)
        print(totalGBP)
        print(convertedRupee)

    if userInput == '5':
        System.exit()

conversionOutput()

So I have this code for a currency converter I'm trying to create. I have been struggling on this for a while now on a few different errors, most of which have been sorted out now, except when I now run this program I get the error:

'float' object is not iterable on the line 'for e in euros:', 'for d in dollars', for y in yen' and 'for r in rupee'.

I have an idea of whats causing the error, I just don't know what I would need to change in order to make this program run perfectly. Any help on this would be really useful.

Because euros is a number, not a list, and I think you can get GBP to euros just:

def euroConversion():
    for x in GBPvalues:
        euros = x * 114 / 100
        convertedEuros.append(euros)
    # or
    convertedEuros += [x * 114 / 100 for x in GBPvalues]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM