3

I would prefer that the 2 lines that print new balance were on the same line instead of 2 separate lines. How do i do this?

def main():

    # Initial balance
    balance = input ('Wallet balance: ')

    # Withdraw amount from wallet balance
    withdraw = input ('Withdraw amount: ')

    # Withdraw process and new balance display
    if withdraw > 0:
        new = balance - withdraw
        print ('Your new balance: ')
        print new

main()
  • The task that you want to complete is simple and is done as stated above by using the ',' x = "123" y = "456" print x,y The problem is usually when you use a loop and inside it you want to use a print. Then in order to get the output in one line you can do: import sys for i in "123456": sys.stdout.write(i) and you get it in one line. – billpcs Jun 27 '14 at 22:14

1 Answers1

4

Separate the values with a comma:

print 'Your new balance:', new

See a demonstration below:

>>> new = 123
>>> print 'Your new balance:', new
Your new balance: 123
>>>
>>> print 'a', 'b', 'c', 'd'
a b c d
>>>

Note that doing so automatically places a space between the values.

  • Damn, 8 seconds ahead of me! – Michael Lorton Jun 27 '14 at 22:05
  • WOW this site is so quick with help, thanks! and a quick question, are the parenthesis even necessary that i have in my code? – MrSassyBritches Jun 27 '14 at 22:07
  • @TollyBear - Not in Python 2.x, where [`print` is a statement](https://docs.python.org/2/reference/simple_stmts.html#grammar-token-print_stmt). You need them in Python 3.x though because [`print` is a function](https://docs.python.org/3/library/functions.html#print). –  Jun 27 '14 at 22:07
  • how can i make this indefinitely repeat as needed by the user? – MrSassyBritches Jun 27 '14 at 22:34
  • @iCodez no sorry i wasn't clear, i mean so they can repeat the process starting at ' # Withdraw amount from wallet balance withdraw = input ('Withdraw amount: ') # Withdraw process and new balance display if withdraw > 0: new = balance - withdraw print ('Your new balance: '), new ' – MrSassyBritches Jun 27 '14 at 22:43
  • @TollyBear - Oh. Well, in that case, you probably want a [`while` loop](https://docs.python.org/2/reference/compound_stmts.html#while) to repeat the code until a certain condition is met. There are many posts here on SO explaining how to do this. If you get stuck, feel free to ask another question so that you can receive proper help. :) –  Jun 27 '14 at 22:46