Is there a way to convert a NoneType into a String?
Question:
Is there a way to convert a NoneType that the user inputs into a string, in python?
This is my code:
topping = print(input("What topping would you like on your pizza?"))
requested_toppings = []
requested_toppings.append(topping)
print("Adding" + topping + "...")
print("n Finished Making Your Pizza!")
I have tried using str(topping)
between the requested toppings list and the append. What am I doing wrong? Thanks!
Answers:
You do not need print()
around the input function. The input function already prints the message as a prompt. The reason you are getting None
is because the print function does not return anything, whereas the input function does.
Here is the corrected code:
topping = input("What topping would you like on your pizza?")
requested_toppings = []
requested_toppings.append(topping)
print("Adding" + topping + "...")
print("n Finished Making Your Pizza!")
It is possible to "convert" None
to str
-> str(None)
which will result a string 'None'
. This is not what you want here.
You have a problem in your code – input
returns a string, but you print it. print
doesn’t return a value, so after topping = print(input("What topping would you like on your pizza?"))
, topping
will be equal to None
.
Instead, you should save the value from the input
in the topping
variable:
topping = input("What topping would you like on your pizza?")
If no input is given, you will receive an empty string from the user, so you still won’t need to convert None
to a string.
Is there a way to convert a NoneType that the user inputs into a string, in python?
This is my code:
topping = print(input("What topping would you like on your pizza?"))
requested_toppings = []
requested_toppings.append(topping)
print("Adding" + topping + "...")
print("n Finished Making Your Pizza!")
I have tried using str(topping)
between the requested toppings list and the append. What am I doing wrong? Thanks!
You do not need print()
around the input function. The input function already prints the message as a prompt. The reason you are getting None
is because the print function does not return anything, whereas the input function does.
Here is the corrected code:
topping = input("What topping would you like on your pizza?")
requested_toppings = []
requested_toppings.append(topping)
print("Adding" + topping + "...")
print("n Finished Making Your Pizza!")
It is possible to "convert" None
to str
-> str(None)
which will result a string 'None'
. This is not what you want here.
You have a problem in your code – input
returns a string, but you print it. print
doesn’t return a value, so after topping = print(input("What topping would you like on your pizza?"))
, topping
will be equal to None
.
Instead, you should save the value from the input
in the topping
variable:
topping = input("What topping would you like on your pizza?")
If no input is given, you will receive an empty string from the user, so you still won’t need to convert None
to a string.