Print Statement Inside of Input Returns with a "None"

print statement inside of input returns with a none

input takes a prompt string as its argument, which it will print automatically, but print returns None; it is this that gets printed by input. Your code is equivalent to:

prompt = print(...) # prompt == None
ans = int(input(prompt))

Instead, use str.format to build the prompt and pass it straight to input:

ans = int(input('{0}x{1}='.format(multi, num)))

input() is always producing None

This line:

Grade_in=input(print("Enter Your Grade (Only Upper Case):"))

should be:

Grade_in=input("Enter Your Grade (Only Upper Case):")

Your original code is equivalent to this:

ret = print("Enter Your Grade (Only Upper Case):")
Grade_in = input(ret)

print always returns None, so ret is None, so your call to input prints out None.

You don't need the print at all, since input already prints out what you pass into it.

Code prints None and then asks for the input. Why?

You have a function inside another function:

input(print())

Therefore, what is inside the brackets will execute first. Since a print statement does not return anything, the input() function will not recieve any value, which will look like:

input(None)

Since the input() function prints anything in between its brackets, it prints "None", and following that on the same line, it waits for your input. Then, after you enter a string and hit the enter key, that statement is over, because you do not store it anywhere. If you are in the shell, then it will print the line you input in between apostrophes.

Why is this printing 'None' in the output?

Because there are two print statements. First is inside function and second is outside function. When a function doesn't return anything, it implicitly returns None.

Use return statement at end of function to return value.

e.g.:

Return None.

>>> def test1():
... print "In function."
...
>>> a = test1()
In function.
>>> print a
None
>>>
>>> print test1()
In function.
None
>>>
>>> test1()
In function.
>>>

Use return statement

>>> def test():
... return "ACV"
...
>>> print test()
ACV
>>>
>>> a = test()
>>> print a
ACV
>>>

Input returns None when using function to print a string with a delay

You can write a single, generic with_delay function that will run an arbitrary function, with the given argument, after a delay. You can have it default to print to simulate print_with_delay.

def with_delay(text, f=print):
delay = len(text) / 100 + 1.2
time.sleep(delay)
return f(text)

def start_game():
username = with_delay("Hello adventurer, I am Tony Tales, and what is your name?", input)
with_delay(f"Nice to meet you {username}.")
with_delay("They call me Tony Tales, because I am a very talented tale teller.")
create_story = with_delay("I can sense greatness from you. Would you like to create a story with me? [Y/N]")

Python Script returns unintended None after execution of a function

In python the default return value of a function is None.

>>> def func():pass
>>> print func() #print or print() prints the return Value
None
>>> func() #remove print and the returned value is not printed.
>>>

So, just use:

letter_grade(score) #remove the print

Another alternative is to replace all prints with return:

def letter_grade(score):
if 90 <= score <= 100:
return "A"
elif 80 <= score <= 89:
return "B"
elif 70 <= score <= 79:
return "C"
elif 60 <= score <= 69:
return "D"
elif score < 60:
return "F"
else:
#This is returned if all other conditions aren't satisfied
return "Invalid Marks"

Now use print():

>>> print(letter_grade(91))
A
>>> print(letter_grade(45))
F
>>> print(letter_grade(75))
C
>>> print letter_grade(1000)
Invalid Marks


Related Topics



Leave a reply



Submit