Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Just getting started
#1
Error:
TypeError: cannot concatenate 'str' and 'int' objects on line

when i have the following
balance = 1000

type_of_trans = input("Would you like to make a withdrawal or deposit?: " )

how_much = int(input("How much? "))

if type_of_trans == "withdrawal":
    print balance - "how_much"
if type_of_trans == "deposit":
    print balance + "how_much"
else:
    print "Invalid transaction"
Reply
#2
please put code tags around your code. highlight just the code and click on the # icon.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#3
Please use the correct tags...
In this case is easy to see what happens, but without indentation in python might be impossible to help you.
In this case you are mixing integer and string because print balance - "how_much" must be:
    print(balance - how_much)
As "how_much" is a string, how_much is a variable. Same applies to the python balance + "how_much"

Also notice that if you are using python3 --recommended if you are learning-- print is a function and needs the parenthesis in the call.
Reply
#4
# Write your program below...
balance = 1000


type_of_trans = input("Would you like to make a withdrawal or deposit?: " )

how_much = int(input("How much? "))



if type_of_trans == "withdrawal":
print (balance - how_much)


elif type_of_trans == "deposit":
print (balance + how_much)
else:
print "Invalid transaction"

if how_much > balance:
print "you cant' have a negative balance"

# now i'm supposed to make it say "you cant have a negative balance" if the withdrawal is more than the balance by using a if else statement. i'm not sure how to do that

Reply
#5
Use proper tags when posting code, you've been warned more than once. Here is BBCode help.

The instructions for what you need to do could hardly be more explicit. Show what you have tried and explain what is wrong, if answer is not correct or you get errors.
Reply
#6
# Write your program below...
balance = 1000


type_of_trans = input("Would you like to make a withdrawal or deposit?: " )

how_much = int(input("How much? "))



if type_of_trans == "withdrawal":
    print (balance - how_much)
    
    
elif type_of_trans == "deposit":
    print (balance + how_much)
else:
    print "Invalid transaction"
    
if how_much > balance:
    print "you cant' have a negative balance"

# i need to now have it say "You can't have a negative balance" if the withdrawal amount is more than the balance using a if else statement i'm not sure how to do that. 

when i put in what i think it should be. I get the -balance and the message "you can't have a negative balance" the instructions only wants the message not the balance in this case
Reply
#7
You are close.

Try calculating the new balance after each withdrawal and deposit.
Print the Old Balance, Transaction Type, How Much, and New Balance On One Line.
If the 'New Balance' is less than zero, then you can print your 'negative balance' not allowed.

After you are done, try allowing 'd' or 'deposit', and similar for withdrawal.
After that, try a loop to allow more than one transaction.

Lewis
To paraphrase: 'Throw out your dead' code. https://www.youtube.com/watch?v=grbSQ6O6kbs Forward to 1:00
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Cannot get started standenman 4 1,222 Feb-22-2023, 05:25 PM
Last Post: standenman
  Need help i just started the whole thing gabriel789 16 3,232 Sep-12-2022, 08:04 PM
Last Post: snippsat
  Can't even get started dr6 1 1,623 Aug-18-2020, 04:38 PM
Last Post: Larz60+
  Getting started mba_110 0 1,736 Jan-18-2019, 05:23 PM
Last Post: mba_110
  Getting Started wargasme 5 3,414 Jun-19-2018, 07:25 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020