0

Im having trouble with this code Ive tried to fix it but I justt can't seem to figure out what the problem is can someone please help

def main():

    print("~~~~~~~~~~~~~~~~~~~~~~~~~~~")
    print("Welcome to the Movie Store!")
    print("~~~~~~~~~~~~~~~~~~~~~~~~~~~")
    budget = int(input("What is your budget?"))
    print("Your budget is " + budget + "$.")
    sale = (200 - budget)

    if(budget < 200):
        print("print("If you spend $" + sale + "you'll qualify for a gift!")
    else:
        print("You qualify for a free gift!")

main()

Error

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-163-65823cbb31f3> in <module>
      3 print("~~~~~~~~~~~~~~~~~~~~~~~~~~~")
      4 budget = int(input("What is your budget?"))
----> 5 print("Your budget is " + budget + "$.")
      6 sale = (200 - budget)
      7 

TypeError: can only concatenate str (not "int") to str
oguz ismail
  • 1
  • 16
  • 47
  • 69
  • This is a duplicate of [Getting a TypeError: can only concatenate str (not "int") to str](https://stackoverflow.com/questions/51252580/getting-a-typeerror-can-only-concatenate-str-not-int-to-str) – Trenton McKinney Jun 19 '20 at 18:43
  • 1
    Bypassing duplicate chain; Possible duplicate of [How can I concatenate str and int objects?](https://stackoverflow.com/questions/25675943/how-can-i-concatenate-str-and-int-objects) – pppery Jun 20 '20 at 00:16

8 Answers8

1

Replace budget with str(budget) in print("Your budget is " + budget + "$.").

Osadhi Virochana
  • 1,294
  • 2
  • 11
  • 21
1

The error says:

TypeError: can only concatenate str (not "int") to str

You can't join together a string and budget which is an integer.

Change:

print("Your budget is " + budget + "$.")

To:

print("Your budget is " + str(budget) + "$.")
1

def main():

print("~~~~~~~~~~~~~~~~~~~~~~~~~~~")
print("Welcome to the Movie Store!")
print("~~~~~~~~~~~~~~~~~~~~~~~~~~~")
budget = int(input("What is your budget?"))
print("Your budget is " + budget + "$.")
sale = (200 - budget)

if(budget < 200):
    print("print("If you spend $" + sale + "you'll qualify for a gift!")
else:
    print("You qualify for a free gift!")

main()

You are getting this error because you didn't convert the budget from int to string before printing. As we can only concatenate a string to a string .

They are many ways like:

1》Convert budget to str before printing:

   print("Your budget is " + str(budget) + "$.")

2》Use formatting in str:

 print("Your budget is  {} $" .format(budget))

3》Use f-string :

 print(f"Your budget is {budget} $.")

You have used print here 2times and here you also have to convert sale to string before printing using any of the above mentioned ways:

if(budget < 200):
    print("print("If you spend $" + sale + "you'll qualify for a gift!")
else:
    print("You qualify for a free gift!")

Hope you found this solution useful ...!!

0
print("~~~~~~~~~~~~~~~~~~~~~~~~~~~")
print("Welcome to the Movie Store!")
print("~~~~~~~~~~~~~~~~~~~~~~~~~~~")
budget = input("What is your budget?")
print("Your budget is " + budget + "$.")
budget = int(budget)
sale = (200 - budget)

if(budget < 200):
    print("If you spend")
else:
    print("You qualify for a free gift!")

You don't need to switch budget to a variable right away since you are going to print it right after the input( You can't print strings with integer's). So i changed it to convert to an integer right before the program needs it. Hope this Helps You!

0

An alternative to concatenating is to use f-strings. I found these examples in the documentation to be quite useful.

print(f"Your budget is {budget}$.")

Here, the budget variable can be of type str, int, or any other data type that can be casted into a string.

The benefit of using f-strings is that you don't have to worry about type conversions at all. Python will do them for you. You can even use results returned from function calls as well.

# Sample code
print(f"This length of this list is {len(lst)}.")

Jake Tae
  • 1,681
  • 1
  • 8
  • 11
0

Use this:

print("Your budget is ",budget,"$.")
yasnil
  • 77
  • 8
0

The error message provides some useful information. It will even tell you exactly where the error happened. In this case, it tells you there is a TypeError on line 5:

Traceback (most recent call last):
  File "C:\Users\Chris\Desktop\deleteme\deleteme.py", line 5, in <module>
    print("Your budget is " + budget + "$.")
TypeError: must be str, not int

If you scroll to line 5, you'll see that it is:

print("Your budget is " + budget + "$.")

So, the problem is that you are trying to add the types str + int + str. That doesn't work. You'll have to convert the int to a string. Think of it like str + str(int) + str, or in your code:

print("Your budget is " + str(budget) + "$.")

Alternatively, the way I would do it is by using 'f-strings'. You just need to put an f before the first quotation mark, and your variables into curly braces ({}). This way, you won't even need to use str():

print(f"Your budget is {budget} $.")
ChrisCrossCrash
  • 651
  • 8
  • 18
0

You can use formated string in python: You can use strng + int + strng this is not a method to print in python you can print by formated string like : print(f"Your budget is {budget }$.")

Arjav
  • 31
  • 1