with open("info.txt", "r") as file:
for line in file:
(name, weight, height) = line.strip().split(", ")
if (not name) or (not weight) or (not height):
continue
bmi = int(weight) / ((int(height) / 100) **2)
result = ""
if 25 <= bmi:
result = "과체중"
elif 18.5 <= bmi:
result = "정상 체중"
else:
result = "저체중"
print('\n'.join([
"이름: {}",
"몸무게: {}",
"키: {}",
"BMI: {}",
"결과: {}"
]).format(name, weight, height, bmi, result))
print()
error message:
Traceback (most recent call last): File "c:\Users\cnhan\My_project\python\tempCodeRunnerFile.py", line 4, in (name, weight, height) = line.strip().split(", ") ValueError: too many values to unpack (expected 3)
CodePudding user response:
Check what is the length of array returned by line.strip().split(", ")
. The error occured because it returned more than 3 values (name, weight, height).
If it happens that some lines may return more than 3 values, but you want only the first three then you can add the *
operator in the end.
Like this
(name, weight, height, *_) = line.strip().split(", ")