Typeerror can only concatenate str not nonetype to str

0 points

Submitted by Zh Q

over 7 years

Hi, I would appreciate any help with my code as followed

def hotel_cost(days): return 140days def plane_ride_cost(city): if(city==”Charlotte”): return 183 elif(city==”Tampa”): return 220 elif(city==”Pittsburgh”): return 222 elif(city==”Los Angeles”): return 475 def rental_car_cost(days): cost=40days if(days>=7): cost=cost-50 elif(days>=3): cost=cost-20 return cost def trip_cost(days,city,spending_money): sum=hotel_cost(days)+plane_ride_cost(city)+rental_car_cost(days)+spending_money return sum

And I was kept telling something like ‘Oops, try again. trip_cost(‘Pittsburgh’, 4, 0) raised an error: cannot concatenate ‘str’ and ‘NoneType’ objects’

I tried to change “” to ‘’ but it didn’t work.

Thanks again.

Answer 54a1a8fe937676dae7015774

Hi, I suggest that you define a function called spending_money(money) that returns money. Then, you simply add money to the arguments of the trip_cost function and also add spending_money(money) to the sum.

Regards, Matt

points

over 7 years

Answer 54a5f2b276b8fec6290020f4

Hi Calvin,

I think that you should make your spending_money function return money so that you actually get the function to work.

Regards, Matt

points

over 7 years

Answer 54a32ac476b8fec921018063

Im having the exact same problem. There seems to be an issue with the “plane ride” part of the code but that is a given and worked fine in a previous problem, so I don’t know why it won’t work now. my code is

def hotel_cost(nights): return 140 * nights

def plane_ride_cost(city): if (city == “Charlotte”): return 183 elif (city == “Tampa”): return 220 elif (city == “Pittsburgh”): return 222 elif (city == “Los Angeles”): return 475

def rental_car_cost(days):
if days >= 7: return (40days)-50 elif days >= 3: return (40days)-20 elif days < 3: return (40*days) def spending_money(money): return money

def trip_cost(days, city, spending_money): return hotel_cost(days) + plane_ride_cost(city) + rental_car_cost(days) + spending_money(money)

points

over 7 years

Answer 54a130d076b8fe9670013922

In your def tripcost statement, spendingmoney is one word. In your sum statement near the end, spending_money has an underscore between the two words.

points

Typeerror can only concatenate str not nonetype to str

over 7 years

Answer 54a454c095e3788cec0002be

Having similar problems, hope you can help. It’s telling me that I need an indent on my trip_cost function.

def hotel_cost(nights): return 140 * nights

def plane_ride_cost(city): if city == “Charlotte”: return 183 elif city == “Tampa”: return 220 elif city == “Pittsburgh”: return 222 elif city == “Los Angeles”: return 475

def rental_car_cost(days): rental_car_cost = days * 40 if days >= 7: rental_car_cost -= 50 elif days >= 3: rental_car_cost -= 20 return rental_car_cost

def spending_money(money):

def trip_cost(city,days,money): sum = rental_car_cost(days) + hotel_cost(days) + plane_ride_cost(city) + spending_money(money) return sum

points

over 7 years

Answer 54be92c351b887d2330029e1

points

Typeerror can only concatenate str not nonetype to str

over 7 years

can only concatenate str (not NoneType) to str error

Questions : can only concatenate str (not NoneType) to str error

2022-09-17T19:06:31+00:00 2022-09-17T19:06:31+00:00

808

I am making a ROBLOX account checker using anycodings_python-3.x robloxpy. (Python 3.x) And when i execute anycodings_python-3.x the code it gives me this error:

Traceback (most recent call last):
  File "c:\Users\Korisnik\Desktop\project\project.py", line 16, in <module>
    print("\nRAP: " + RapFetch)
TypeError: can only concatenate str (not "NoneType") to str

Here is the code:

import robloxpy
import time

UserID = input("Enter your ROBLOX ID: ")

NameFetch = robloxpy.GetName(UserID)
RapFetch = robloxpy.GetUserRAP(UserID)
LimitedValueFetch = robloxpy.GetUserLimitedValue(UserID)
FollowerFetch = robloxpy.GetFollowersCount(UserID)
FollowingFetch = robloxpy.GetFollowingCount(UserID)
BannedFetch = robloxpy.IsBanned(UserID)
AccountAgeFetch = robloxpy.AccountAgeDays(UserID)

print("\nUsername: " + NameFetch)
time.sleep(0.1)
print("\nRAP: " + RapFetch)
time.sleep(0.1)
print("\nLimited Value: " + LimitedValueFetch)
time.sleep(0.1)
print("\nFollowers: " + FollowerFetch)
time.sleep(0.1)
print("\nFollowing: " + FollowingFetch)
time.sleep(0.1)
print("\nBanned: " + BannedFetch)
time.sleep(0.1)
print("\nAccount Age (In Days): " + AccountAgeFetch)

input("\nPress ENTER to exit console")

Total Answers 1

24

Answers 1 : of can only concatenate str (not NoneType) to str error

My guess is some players just don't have anycodings_python-3.x a RAP (recent average price) attribute anycodings_python-3.x so it returns None -- either you can anycodings_python-3.x replace this by a '' or '0':

RapFetch = robloxpy.GetUserRAP(UserID) or ''

or you can just straight out print out anycodings_python-3.x the None:

print(f"\nRAP: {RapFetch}")

Might have to apply this to the other anycodings_python-3.x attributes as well.

0

2022-09-17T19:06:31+00:00 2022-09-17T19:06:31+00:00Answer Link

mRahman

How do you fix TypeError can only concatenate str not NoneType to STR?

The Python "TypeError: can only concatenate str (not "NoneType") to str" occurs when we try to concatenate a string and a None value. To solve the error, correct the assignment or check if the variable doesn't store a None value before concatenating.

How do you fix TypeError can only concatenate str not list to STR?

The Python "TypeError: can only concatenate str (not "list") to str" occurs when we try to concatenate a string and a list. To solve the error, access the list at a specific index to concatenate two strings, or use the append() method to add an item to the list.

Can only concatenate str not function to str?

Conclusion # The Python "TypeError: can only concatenate str (not "set") to str" occurs when we try to concatenate a string and a set. To solve the error, use a formatted string literal, or use the add() method to add an item to the set.

Can only concatenate str not tuple to STR in Python?

The Python "TypeError: can only concatenate str (not "tuple") to str" occurs when we try to concatenate a string and a tuple. To solve the error, access the tuple at a specific index to concatenate two strings, or use a formatted string literal.