0

I need to write a conventer (Celsius to Kelvin,Kelvin to Celsius...). User write: a - degrees(for example 0), b - for example: Kelvin, c - for example: Celsius.

Exercise: Input Format There are a few input lines. Each line consist of three words. First word indicates starting temperature. Second word indicates starting scale. Third word indicates target scale.

Constraints Possible scales are: {"Kelvin", "Celsius", "Fahrenheit"}. Temperatures are up to 1000 degrees. Whenever initial temperature is below Absolute zero output "NO". Number in the output should maintain precision up to second decimal place.

Output Format For each input line one number that is the calculated temperature (with precision of two decimal places) or word "NO".

This is for hackerank

Error:

Traceback (most recent call last):
  File "C:/Users...", line 4, in <module>
    a, b, c = input().split()
ValueError: not enough values to unpack (expected 3, got 0)

Code:

while True:
    a, b, c = input().split()
    if int(a) < 0:
        print('NO')
    else:

Leave the loop when user stop writing.

3
  • while True: a, b, c = input().split() if int(a) < 0: print('NO') else: Commented Oct 21, 2019 at 16:50
  • You need to specify the meaning of "when user stops writing" in programmatic terms. Then test for that condition. Since you haven't told us what it means, we can't "correct" your code. Commented Oct 21, 2019 at 16:52
  • Welcome to StackOverflow. Please follow the posting guidelines in the help documentation, as suggested when you created this account. On topic, how to ask, and ... the perfect question apply here. Our immediate need is that you fully describe your problem in the posting; not in a comment, not under an answer, not using an off-site link with no explanation. Commented Oct 21, 2019 at 17:09

1 Answer 1

1

Taking a guess at one possible solution:

a, b, c = input().split()

This code requires exactly three non-blank fields in the line. You could test for that;

user_stuff = input().split()
if len(user_stuff) != 3:
    break
a, b, c = user_stuff

Your link implies that you're reading from a file piped to stdin, rather than from live user input. In that case, you have to treat sys.stdin as a file:

from sys import stdin

for line in stdin:
    a, b, c = line.split()
    ...
Sign up to request clarification or add additional context in comments.

1 Comment

It works but link I have the same problem, when i tried to do it with ''try/except''

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.