【问题标题】:Input multiple lines in pycharm [duplicate]在pycharm中输入多行[重复]
【发布时间】:2016-09-22 11:21:36
【问题描述】:

当我尝试输入试图找到 数组中数字的位置,我想在 我该怎么做,有没有更简单的方法?谢谢你。我明白了 尝试在pycharm的控制台中执行时出现以下错误

例子

输入

2
4
1 2 3 4
3
5
10 90 20 30 40
40

 # Returns index of x in arr if it is present,
 # else returns -1
 def search(arr, x):
     n = len(arr)
     for j in range(0,n):
         if (x == arr[j]):
             return j
     return -1

 # Input number of test cases
 t = int(raw_input())

 # One by one run for all input test cases
 for i in range(0,t):

     # Input the size of the array
     n = int(raw_input())

     # Input the array
     arr = map(int, raw_input().split())

     # Input the element to be searched
     x = int(raw_input())

     print(search(arr, x))

在 pycharm 控制台上我的输入:

2  
4
1 2 3 4  
3  
5  
10 90 20 30 40  
40  

输出

 Traceback (most recent call last):  
 File "<input>", line 8, in <module>  
 ValueError: invalid literal for int() with base 10: '2\n 41 2 3 4\n 3\n 5\n 10 90 >20 30 40\n 40'

【问题讨论】:

    标签: python pycharm


    【解决方案1】:

    因为输入字符串无法转换为 int ('2\n 41 2 3 4\n 3\n 5\n 10 90 >20 30 40\n 40') 您应该使用 '\n' 和 ' ' 拳头解析输入值,然后转换为 int 数字

    【讨论】:

    • 必须有另一种方式,我们可以只输入值而不是在 pycharm 中解析它们。如何在竞争性项目中解决这个问题
    猜你喜欢
    • 2015-04-29
    • 2015-04-14
    • 1970-01-01
    • 2015-08-15
    • 2015-04-29
    • 1970-01-01
    • 2013-02-27
    • 1970-01-01
    • 2021-11-01
    相关资源
    最近更新 更多