【问题标题】:Not able to get a input from the stdin in python 3无法从 python 3 中的标准输入获取输入
【发布时间】:2020-01-09 08:23:04
【问题描述】:

我正在从控制台或工具使用自动输入之类的平台学习 python,有人提到使用 STDIN 输入,我正在尝试 sys.stdin.readline()sys.stdin.readline().split() 但似乎没有工作,请帮助我。

谢谢

提示是这样的..

Sample Test Cases

**Sample Input**
=======================================
3

1 9


22 31


90 103
========================================
Sample Output
========================================

Difference Not in Range

[22, 23, 24, 25, 26, 27, 28, 29, 30, 31]

Out of Range

========================================

我正在尝试sys.stdin.readline()sys.stdin.readline().split() 试图提供手动输入 试过int(input())

代码:

import sys

# Read the variable from STDIN
#begin is the beginning and end is for storing the end value
#taking the user input for the start and end points of the integer
t=int(sys.stdin.readline())
l=[]
while t:
   n=1
   a=int(sys.stdin.readline().split())
   start=int(a[0])
   end=int(a[1])
   if (end-start<10 and end-start>10 and start<=0,end>=100):
       print("Difference Not in Range!")
       if start<=0 and end>=100:
           print("Out of Range!")
   else:
       while start<end:
           start=start+1
           l.append(start)
# Output the variable to STDOUT
       STDOUT=print(l)`

【问题讨论】:

    标签: python input stdin


    【解决方案1】:

    您正在尝试将字符串列表转换为整数。

    a=int(sys.stdin.readline().split())
    

    也许这样就可以了:

    a=str(sys.stdin.readline()).split()
    

    因为您将以下两行中的它转换为整数

    【讨论】:

      【解决方案2】:

      您从标准输入正确读取,因此如果您没有看到输出,则问题出在程序逻辑上。

      这是您现在对标准输入的最小输入的内容:

      line = sys.stdin.readline()
      print(int(line))
      
      % echo 1  | python readlines.py
      1
      

      这里有一个稍微不同的方法:

      numbers = sys.stdin.read().split()
      print(numbers)
      
      % echo 1 2 3 2 | python readlines.py
      ['1', '2', '3', '2']
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多