James Hulse
James Hulse

Reputation: 25

Function return formatting

Simple ATM simulation exercise - Need to return some information including strings and integers however return is spitting out the whole line of code, not just the string and integers combined.

CASHPOINT CODE

from SimpleCashPoint_v2 import cashpoint


print('\nTEST-EXAMPLE 1')

result = cashpoint('1234',3415.55)
print('\n---------\nRESULT:', result)
print('-' * 40, '\n')

cahspoint function code (in file SimpleCashPoint_v2)

elif trans_type == '2' : 
    withdraw = float(input('Amount to withdraw: '))
    result = ('\nYou have withdrawn ', withdraw, ' your remaining balance is ', (balance-withdraw),'£')

    return result

#I expect an output of:

In[36]result

Out[36] You have withdrawn 10 your remaining balance is 50 £

I get an output of

In[36]result

Out[36]: ('\nYou have withdrawn ', 10, ' your remaining balance is ', 50, '£')

Upvotes: 0

Views: 47

Answers (2)

Jason Chia
Jason Chia

Reputation: 1145

You're making a tuple when you made result.

type(result)
<class 'tuple'>

So I think if you combine your elements into a single string, it would work.

One very naive way would be the following:

withdraw = ...
result = "/nYou have withdrawn" + str(withdraw) + "your remaining balnce is" + str(balance-withdraw) + "$"
return result

Upvotes: 0

Dan
Dan

Reputation: 1587

You can replace:

result = ('\nYou have withdrawn ', withdraw, ' your remaining balance is ', (balance-withdraw),'£')

with:

result = "\nYou have withdrawn {} your remaining balance is {} £".format(withdraw, balance-withdraw)

Have a search for string formatting, e.g. https://realpython.com/python-string-formatting/

Upvotes: 1

Related Questions