r/pythonhelp • • Jul 11 '22

input validation, while loop

# Code below, attempting to check input validation for user, I was able to make sure value > 0

# however, if user doesn't enter anything or enters a string, I get a traceback error

# Any advice on how to fix this?

resistances = [12, 13, 81, 56, 39, 27, 5, 16]
currents = []
powers = []

for resistance in resistances:
temp = eval(input("Enter a current: "))
while temp < 1:
print("Error, current number has to be greater than 0.")
temp = eval(input("Enter a current: "))
currents.append(int(temp))
powers.append(currents[-1] * currents[-1] * resistance)

bundle_number = zip(resistances, currents, powers)
sorted_numbers = sorted(bundle_number)

# print(sorted_numbers)
print()
print("\t", "Resistance", "Current", "Power", sep="\t")
print("\t", "----------", "-------", "-----", sep="\t")
print()
for (resistance, current, power) in sorted_numbers:
print("\t", resistance, current, power, sep="\t\t")
print("\t", "----------", "-------", "-----", sep="\t")
print("Totals:", sum(resistances), sum(currents), sum(powers), sep="\t\t")

0 Upvotes

2 comments sorted by

View all comments

1

u/carcigenicate Jul 11 '22

Format the code and show the full error.