0

I want the user to input values only to the needed variables (Eg: If you need to get the power ignore the input power and only input to the remaining variables). I would really appreciate the help.

Code:

Power = int(input("Power: "))
Current = int(input("Cuurent: "))
Voltage = int(input("Voltage: "))
def P(C,V):
    return C * V
def C(P,V):
    return P / V
def V(P,C):
    return P / C
if Power == None:
    print(P(Current,Voltage))
elif Current == None:
    print(C(Power,Voltage))
else:
    print(V(Power,Current))

Error:

Traceback (most recent call last):
  File "C:\Users\tharu\Desktop\Python.py", line 1, in <module>
    Power = int(input("Power: "))
ValueError: invalid literal for int() with base 10: ''
Tharu
  • 188
  • 2
  • 14
  • Did you try using an `if` statement to check if the string is empty before trying to convert it to an integer? – mkrieger1 Oct 21 '20 at 16:41
  • 2
    Does this answer your question? [Default values on empty user input](https://stackoverflow.com/questions/22402548/default-values-on-empty-user-input) – mkrieger1 Oct 21 '20 at 16:44

2 Answers2

2

Don't convert it to an int immediately and check to see the truthiness of the input

Power = input("Power: ")
Current = input("Cuurent: ")
Voltage = input("Voltage: ")
def P(C,V):
    return C * V
def C(P,V):
    return P / V
def V(P,C):
    return P / C
if not Power:
    print(P(int(Current),int(Voltage)))
elif not Current:
    print(C(int(Power),int(Voltage)))
else:
    print(V(int(Power),int(Current)))

>>> Power:
>>> Cuurent: 2
>>> Voltage: 20
40

An empty string will report False so you can use that fact to know which input was left blank. Once you know that, then you can convert the others to an int within that portion of the code.

Chrispresso
  • 3,660
  • 2
  • 19
  • 31
  • Don't use `bool` in boolean context, it's useless. Just do `if mystring:` to test if it's not empty, or `if not mystring:` to test if it's empty. – Thierry Lathuille Oct 21 '20 at 16:42
-3

You are getting this error because you are leaving the input blank. You need to put an integer value as input else you can condition the input function or use command line arguments.