I'm new to coding in python and I was filling out some code that required a number of inputs. One thing it asked for was for the program to perform an action if the user pressed the enter key and did not type in any input. My question is how you would get python to check for that. Would it be:
if input == "":
#action
Or is it something else? Thank you for the help.
Edit: Here is what my code currently looks like for reference.
try:
coinN= int(input("Enter next coin: "))
if coinN == "" and totalcoin == rand:
print("Congratulations. Your calculations were a success.")
if coinN == "" and totalcoin < rand:
print("I'm sorry. You only entered",totalcoin,"cents.")
except ValueError:
print("Invalid Input")
else:
totalcoin = totalcoin + coinN
I know this question is old, but I'm still sharing the solution to your problem as it could be a helpful hand to others. To detect no input in Python, you actually need to detect for "End of File" error. Which is caused when there is no input:
This can be checked by the following piece of code:
Hope this helps.
Actually an empty string would be
Instead of
The latter is a space character
Edit
A few other notes
Don't use
input
as your variable name that is a Python keywordComparing equality uses
==
instead of=
, the latter is an assignment operator, it attempts to modify the value of the left-hand side.I'm new to python and was looking for a fix to a similar problem. I know this is a really old post but I thought I would have a go at it. If I understand your problem correctly and what you're trying to achieve, this works fine for me. (As long as you don't try and enter a letter!) I posted earlier but it wasn't right, sorry.
EDIT:
what about something like this:
Just another tips:
In python you don't need to do equality test for empty string. Instead please use truth value testing. That is more pythonic.
Truth value testing covers the following test:
Example: