0

I am still pretty new to Python and learning! I searched around and some postings seem too complex for me at this time. Wondering why the car_brandp below is not joining with "and quite expensive" after the else function initiates? The first else line prints fine but it seems like I can't put that message as a variable?

I got the None Type error

car_brand =input ("What is the best car brand? ")
if car_brand == ("Range Rover"):
    print (car_brand + " is the best car brand ever!")
else:
    car_brandp = print (car_brand + " is just personal taste..")
    print (car_brandp + " and quite expensive...")
3
  • 2
    print returns None. car_brandp = print() will always cause car_brandp to be None Commented Apr 8, 2019 at 16:56
  • 1
    print is used to print out to standard output. Here you need an assignment to a variable. car_brandp = car_brand + " is just personal taste..". Commented Apr 8, 2019 at 16:56
  • Or, from 3.6 f'{car_brand} is just personal taste..' (also available in multi-line version that won't render properly in a comment) Commented Apr 8, 2019 at 17:00

3 Answers 3

2

This line:

car_brandp = print (car_brand + " is just personal taste..")

is suppose to be:

car_brandp = (car_brand + " is just personal taste..")
Sign up to request clarification or add additional context in comments.

4 Comments

By the way, it's worth pointing out that the reason why OP was getting NoneType error was because print() returns None
@DroidX86 why would that turn None when I have a string after print () ? I know that if I do print () it prints none, but i had a string in there with a variable from the previous line... its like a mystery to me.. maybe I just don't have the basics down and I should revert =[
@bestcoderNAjk That's the behaviour of print. No matter what argument you give it - it'll send it to stdout and return None to you. programiz.com/python-programming/methods/built-in/print
@DroidX86 interesting.. still learning. Thank you for the info!
1

"print" is a procedure to display something in the console. A procedure differs from a function as it is not meant to return something of value, but rather perform something as a side effect (it will do something useful but you cannot interact with it). You may not assign the return value of the print function as it is meaningless.

Since you are still new to Python, it is a good idea to learn the proper habits early. In particular, PEP8 contains valuable information on style and conventions that most Python developers follow. Such recommendations are optional, but when followed, they help other developers understand your code better.

car_brand = input("What is the best car brand? ")

if car_brand == "Range Rover":
    msg = car_brand + " is the best car brand ever!"
else:
    msg = car_brand + " is just personal taste.."
    msg += " and quite expensive..."

print(msg)

2 Comments

Never knew I can write it this way as well.. I was following a youtube video and a python crash course book. Thanks a lot!
There are even better ways to build strings, I will let you discover them by yourself. Have fun on your journey, it's the most important part. If this answer was useful to you, please consider upvoting and/or accepting it.
0

print() is like a void function in other languages. So, it does something, but it returns nothing (None, in python, like null in other languages).

So, in your next line, you are trying to add

None + "personal taste"

and you get an error because addition of string and None is not defined

So, you options are

  • consecutive prints
  • concatenate strings eg print( str(brand) + "personal taste")
  • formatted strings eg print( f'{brand} personal taste')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.