Jump to content

ok so I have a school assignment due very soon and I am reqiured to write some code using python. The code i've been asked to write is the change between the orignal amount and the expense. The code has failed to work and i've tried almost everything to fix it please help.

(yes i have emaile my teacher but they haven't told me anything that I already know.

The error that oocurs is TypeError: unsupported operand type(s) for -: 'str' and 'int'

 

Here is the code if you could please have a look.

 

name = input("Enter first name: ")
originalAmount = input("Enter amouunt of money you have: ")
cost1 = int(input("How much was your first item? "))
cost2 = int(input("How much was your second item? "))
cost3 = int(input("How much was your third item? "))

expenses = cost1+cost2+cost3

cost=print("You spent a total of $: ",expenses)

change = originalAmount - expenses

cost = print("Welcome " + name ," your total cost is $: ",sum ,"and your change is $: ",change)

Screen Shot 2020-08-08 at 11.20.47 am.png

Link to comment
https://linustechtips.com/topic/1231825-need-help-with-code/
Share on other sites

Link to post
Share on other sites

You forgot to cast your originalAmount to int, just like you did with cost1, 2 and 3.

 

You also are trying to print "sum" instead of "expenses".

FX6300 @ 4.2GHz | Gigabyte GA-78LMT-USB3 R2 | Hyper 212x | 3x 8GB + 1x 4GB @ 1600MHz | Gigabyte 2060 Super | Corsair CX650M | LG 43UK6520PSA
ASUS X550LN | i5 4210u | 12GB
Lenovo N23 Yoga

Link to comment
https://linustechtips.com/topic/1231825-need-help-with-code/#findComment-13901452
Share on other sites

Link to post
Share on other sites

12 minutes ago, igormp said:

You forgot to cast your originalAmount to int, just like you did with cost1, 2 and 3.

 

You also are trying to print "sum" instead of "expenses".

Ok thanks i'll see what i can do. Before I stuff up the code would you have any idea what the code would be to cast the originalAmount to int?

Link to comment
https://linustechtips.com/topic/1231825-need-help-with-code/#findComment-13901482
Share on other sites

Link to post
Share on other sites

-= Topic moved to Programming =-

 

Please mind where you post.

COMMUNITY STANDARDS   |   TECH NEWS POSTING GUIDELINES   |   FORUM STAFF

LTT Folding Users Tips, Tricks and FAQ   |   F@H & BOINC Badge Request   |   F@H Contribution    My Rig   |   Project Steamroller

I am a Moderator, but I am fallible. Discuss or debate with me as you will but please do not argue with me as that will get us nowhere.

 

Spoiler

Character is like a Tree and Reputation like its Shadow. The Shadow is what we think of it; The Tree is the Real thing.  ~ Abraham Lincoln

You have enemies? Good. That means you've stood up for something, sometime in your life.  ~ Winston Churchill

Reputation is a Lifetime to create but takes only seconds to destroy.

Docendo discimus - "to teach is to learn"

 

  

 CHRISTIAN MEMBER 

 
 
 
 
 
 

 

Link to comment
https://linustechtips.com/topic/1231825-need-help-with-code/#findComment-13902124
Share on other sites

Link to post
Share on other sites

name = input("Enter first name: ")

currentAmount = int(input("Enter amount of money you have: "))

cost = int(input("How much was your first item? "))
cost += int(input("How much was your second item? "))
cost += int(input("How much was your third item? "))

print(f"Welcome {name} your total cost is ${cost} and your change is ${currentAmount - cost}\n")

or

name = input("Enter first name: ")

currentAmount = int(input("Enter amount of money you have: "))

print("Enter Item Amounts")

cost = \
    int(input("Item 1: ")) + \
    int(input("Item 2: ")) + \
    int(input("Item 3: "))

print(f"Welcome {name} your total cost is ${cost} and your change is ${currentAmount - cost}\n")

Is cleaner imo. However you were just missing the int conversion on originalAmount.

Link to comment
https://linustechtips.com/topic/1231825-need-help-with-code/#findComment-13902583
Share on other sites

Link to post
Share on other sites

On 8/8/2020 at 10:57 PM, Nayr438 said:

name = input("Enter first name: ")

currentAmount = int(input("Enter amount of money you have: "))

cost = int(input("How much was your first item? "))
cost += int(input("How much was your second item? "))
cost += int(input("How much was your third item? "))

print(f"Welcome {name} your total cost is ${cost} and your change is ${currentAmount - cost}\n")

or


name = input("Enter first name: ")

currentAmount = int(input("Enter amount of money you have: "))

print("Enter Item Amounts")

cost = \
    int(input("Item 1: ")) + \
    int(input("Item 2: ")) + \
    int(input("Item 3: "))

print(f"Welcome {name} your total cost is ${cost} and your change is ${currentAmount - cost}\n")

Is cleaner imo. However you were just missing the int conversion on originalAmount.

Oh my goodneas thanks so much, This has helped out alot I really appreciate it!!!

Link to comment
https://linustechtips.com/topic/1231825-need-help-with-code/#findComment-13910758
Share on other sites

Link to post
Share on other sites

On 8/8/2020 at 6:50 AM, Cosco said:

ok so I have a school assignment due very soon and I am reqiured to write some code using python. The code i've been asked to write is the change between the orignal amount and the expense. The code has failed to work and i've tried almost everything to fix it please help.

(yes i have emaile my teacher but they haven't told me anything that I already know.

The error that oocurs is TypeError: unsupported operand type(s) for -: 'str' and 'int'

 

Here is the code if you could please have a look.

 

name = input("Enter first name: ")
originalAmount = input("Enter amouunt of money you have: ")
cost1 = int(input("How much was your first item? "))
cost2 = int(input("How much was your second item? "))
cost3 = int(input("How much was your third item? "))

expenses = cost1+cost2+cost3

cost=print("You spent a total of $: ",expenses)

change = originalAmount - expenses

cost = print("Welcome " + name ," your total cost is $: ",sum ,"and your change is $: ",change)

 

I know that you've probable already summitted the code, but still, why did you not set up a simple loop so that you could add more than 3 items?, when i ran your code after correcting it,, the output was
Enter first name: Lol
Enter amouunt of money you have: 1000
How much was your first item? 100
How much was your second item? 100
How much was your third item? 200
You spent a total of $:  400
Welcome Lol  your total cost is $:  <built-in function sum> and your change is $:  600

subjectively, i don't know why you put sum function there, it returned <built-in function sum>, was it supposed to return sume value, anyways i refined your code and added a while loop so the user is not restricted to 3 items

name = input("Hi!,What is your name? ")
originalAmount = int(input("Please enter amount of money you have: "))
item_count = int(input("How many items did you buy ? -> "))
n = 1
expenses = 0
while n <= item_count:
    cost_n = int(input("Please enter the cost of item no." + str(n) + "-> "))
    expenses += cost_n
    n += 1
change = originalAmount - expenses

print("I am glad that I could help you ",name ," the total cost of items you bought is ",expenses,
      "$ and you are left with ",change, "$")


I added a while loop and also removed 1(imo) unnecessary print command, it has the same number of lines as yours so, i don't think that the size of text is much of a problem here.

Link to comment
https://linustechtips.com/topic/1231825-need-help-with-code/#findComment-13925808
Share on other sites

Link to post
Share on other sites

On 8/8/2020 at 6:27 PM, Nayr438 said:

name = input("Enter first name: ")

currentAmount = int(input("Enter amount of money you have: "))

cost = int(input("How much was your first item? "))
cost += int(input("How much was your second item? "))
cost += int(input("How much was your third item? "))

print(f"Welcome {name} your total cost is ${cost} and your change is ${currentAmount - cost}\n")

or


name = input("Enter first name: ")

currentAmount = int(input("Enter amount of money you have: "))

print("Enter Item Amounts")

cost = \
    int(input("Item 1: ")) + \
    int(input("Item 2: ")) + \
    int(input("Item 3: "))

print(f"Welcome {name} your total cost is ${cost} and your change is ${currentAmount - cost}\n")

Is cleaner imo. However you were just missing the int conversion on originalAmount.

It indeed is neat, but i am not able to understand the code very well, especially the use of "/", I've never seen it before, can you please explain me?

Link to comment
https://linustechtips.com/topic/1231825-need-help-with-code/#findComment-13925870
Share on other sites

Link to post
Share on other sites

3 hours ago, PiyushBanarjee said:

It indeed is neat, but i am not able to understand the code very well, especially the use of "/", I've never seen it before, can you please explain me?

"\" means the statement is continued on the next line.

 

3 hours ago, PiyushBanarjee said:

I added a while loop and also removed 1(imo) unnecessary print command, it has the same number of lines as yours so, i don't think that the size of text is much of a problem here...

It would probably be simpler to have the user enter a empty value when done

name = input("Enter first name: ")

currentAmount = int(input("Enter amount of money you have: "))

cost, count = 0, 0

print("Enter Item Amounts. When done just press enter")

while True:
    count += 1
    currInput = input(f"Item {count}: ")

    if currInput == "": break
    
    cost += int(currInput)

print(f"Welcome {name}, you purchased {count} items at a total cost of ${cost}. Your change is ${currentAmount - cost}\n")

 

Link to comment
https://linustechtips.com/topic/1231825-need-help-with-code/#findComment-13925881
Share on other sites

Link to post
Share on other sites

9 hours ago, Nayr438 said:

"\" means the statement is continued on the next line.

 

It would probably be simpler to have the user enter a empty value when done


name = input("Enter first name: ")

currentAmount = int(input("Enter amount of money you have: "))

cost, count = 0, 0

print("Enter Item Amounts. When done just press enter")

while True:
    count += 1
    currInput = input(f"Item {count}: ")

    if currInput == "": break
    
    cost += int(currInput)

print(f"Welcome {name}, you purchased {count} items at a total cost of ${cost}. Your change is ${currentAmount - cost}\n")

 

Yes, thanks for correction, btw, since OP didn't say anything, can you tell, why did he put sum function there

Link to comment
https://linustechtips.com/topic/1231825-need-help-with-code/#findComment-13927133
Share on other sites

Link to post
Share on other sites

  • 4 weeks later...
On 8/16/2020 at 12:34 PM, PiyushBanarjee said:

Yes, thanks for correction, btw, since OP didn't say anything, can you tell, why did he put sum function there

Lmao my teacher was gave me 100% so im gonna take it and leave it, and tbh I didn't even have that error (sum function) unless it just didn't appear for me.

Link to comment
https://linustechtips.com/topic/1231825-need-help-with-code/#findComment-14007409
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×