简体   繁体   English

如何摆脱循环 Python

[英]How to escape from loop Python

I need to write a conventer (Celsius to Kelvin,Kelvin to Celsius...).我需要写一个converter(摄氏度到开尔文,开尔文到摄氏度......)。 User write: a - degrees(for example 0), b - for example: Kelvin, c - for example: Celsius.用户写入:a - 度数(例如 0),b - 例如:开尔文,c - 例如:摄氏度。

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"}.约束 可能的尺度是:{"Kelvin", "Celsius", "Fahrenheit"}。 Temperatures are up to 1000 degrees.温度高达1000度。 Whenever initial temperature is below Absolute zero output "NO".每当初始温度低于绝对零 output “NO”。 Number in the output should maintain precision up to second decimal place. output 中的数字应保持精确到小数点后第二位。

Output Format For each input line one number that is the calculated temperature (with precision of two decimal places) or word "NO". Output 格式 对于每个输入行,一个数字是计算的温度(精确到小数点后两位)或单词“NO”。

This is for hackerank这是给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.当用户停止写入时离开循环。

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.您的链接意味着您正在从通过管道传输到stdin的文件中读取,而不是从实时用户输入中读取。 In that case, you have to treat sys.stdin as a file:在这种情况下,您必须将sys.stdin视为文件:

from sys import stdin

for line in stdin:
    a, b, c = line.split()
    ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM