How do you get Python to detect without input
I am new to coding in python and I was filling out some code that requires multiple inputs. One thing he asked was for the program to take action if the user pressed enter and did not enter any input. My question is how do you get python to test this. It will be:
if input == "":
#action
Or is it something else? Thanks for the help.
Edit: This is what my code 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
source to share
Actually the empty line would be
""
Instead
" "
The latter is a spatial symbol
Edit
Several other notes
-
Do not use a
input
Python keyword as a variable name -
Equality comparison uses
==
instead=
, the latter is an assignment operator, it tries to change the value of the left side.
source to share
I know this question is old, but I still share the solution to your problem as it might be a helpful hand for others. To detect missing input in Python, you really need to detect the End of File error. What is called when there is no input:
This can be checked with the following piece of code:
final=[]
while True:
try:
final.append(input()) #Each input given by the user gets appended to the list "final"
except EOFError:
break #When no input is given by the user, control moves to this section as "EOFError or End Of File Error is detected"
Hope it helps.
source to share
A few more tips:
In python, you don't need to do an equality test for an empty string. Use truth value testing instead . It's more pythonic.
if not coinN:
Testing for the value of true covers the following test:
- None
- False
- zero of any numeric type, for example, 0, 0L, 0.0, 0j.
- any empty sequence, for example, '', (), [].
- any empty display like {}.
- Instances of custom classes, if the class defines a method other than zero () or len (), when that method returns an integer 0 or bool False. 1
Example:
>>> s = ''
>>> if not s:
... print is empty'
...
s is empty
>>>
source to share
I am new to Python and was looking for a solution to a similar problem. I know this is a really old post, but I thought I should try it. If I understand your problem correctly and what you are trying to achieve, this works great for me. (As long as you are not trying to enter an email!) I posted earlier but it was not correct, sorry.
totalcoins = None
coinN = None
sum_total = range
while coinN != '0' and totalcoins != '0':
coinN = input("Please enter first amount: ")
if coinN == "":
print("You didn't enter anything")
else:
totalcoins = input("Please enter second amount ")
if totalcoins == "":
print("You didn't enter anything")
else:
sum_total = int(coinN) + int(totalcoins)
if sum_total in range(100):
print('Sorry, you only entered {} cents'.format(sum_total))
else:
if sum_total == 100:
print('The sum of {0} and {1} is = 1 rand '.format(coinN, totalcoins, sum_total))
if sum_total >=100:
print('The sum of {0} and {1} is = {2} rand '.format(coinN, totalcoins, sum_total))
print("\n")
source to share
EDIT:
how about this:
try:
coinN = input("Enter next coin: ")
if coinN.isdigit(): # checks whether coinN is a number
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.")
else:
raise ValueError
except ValueError:
print("Invalid Input")
else:
totalcoin = totalcoin + int(coinN) # convert coinN to int for addition
source to share