- This topic is empty.
Viewing 1 post (of 1 total)
Viewing 1 post (of 1 total)
- You must be logged in to reply to this topic.
IT, Programming, & Web Development › Forums › CS50’s Introduction to Computer Science by Harvard University on Edx › Week 6: Python › MITx 6.00.1x Introduction to Computer Science and Programming Using Python › Week 1: Python Basics › Understanding the ‘None’ issue in Python input
Source: Created with the help of AI tool
The issue you’re encountering with None
appearing in the input likely comes from how you’re using the input()
and print()
functions together in your Python code.
Here’s the part of your code causing the issue:
s = str(input(print('entry: ')))
In this line, print('entry: ')
executes first, printing the message “entry:”. However, print()
doesn’t return any value; it returns None
. Then, the input()
function is called with None
as its argument, which leads to Python printing None
before the user input.
That’s why you see Noneabcdef
—because the None
is printed right before the input is captured.
To fix this, you don’t need to use print()
inside the input()
function. You can rewrite the line like this:
s = input('entry: ')
This will print “entry:” to prompt the user, and then wait for their input without returning None
.
Here’s your corrected code:
s = input('entry: ')
numofvowels = 0
numCons = 0
for char in s:
if char in 'aeiou':
numofvowels += 1
else:
numCons += 1
print('number of vowels: ' + str(numofvowels))
This should work without printing None
, and you’ll be able to enter words without that issue.