简体   繁体   English

为什么我的变量没有被访问? 以及为什么我的输出会聚集在一起而不是在新行中返回

[英]Why is my variable not being accessed? and why do my outputs come together instead of returning in a new line

I am trying to write a program which returns the even and odd strings sliced together.我正在尝试编写一个程序,该程序返回切片在一起的偶数和奇数字符串。 On doing so while compilation the "odd" variable that i have defined inside my "else:" statement is not accessible.在编译时这样做,我在“else:”语句中定义的“odd”变量不可访问。 what could be the problem?可能是什么问题呢? ''' '''

       num = int(input())
        even_L = []
        odd_L = [] 

     for stdin in range(num):
       s = input()
       inputs = [n for n in s]

    for names in inputs:
       for name in range(len(names)):
          if name % 2 == 0:
              even_L.append(s[name])
              even = "".join(even_L)
         else:
              odd_L.append(s[name]) 
              odd = "".join(odd_L)

         new = even + 2 * " " + odd

#I have tried moving the indentation also

    print (new)

''' The error shown is : ''' '''显示的错误是:'''

Traceback (most recent call last):
    File "Solution.py", line 18, in <module>
   new = even + 2 * " " + odd
 NameError: name 'odd' is not defined

''' '''

And

I tried to do it in various ways.我试图以各种方式做到这一点。 But even when i successfully arrange the strings.但即使我成功地安排了字符串。 Since I had to input 2 names and the output should also return two names.因为我必须输入 2 个名字,输出也应该返回两个名字。 But here it returns two names but the names had to be separated right?但这里它返回两个名字,但名字必须分开对吗? ''' '''

let's say two names i.e. Hacker, Rank
the required output is: Hce  akr and Rn  nk

but mine output is Hce akr and HceRn  akrnk

   why does the second output comes paired with first output?

''' '''

My code for the problem is:我的问题代码是:

num = int(input())

even_strings = []
odd_strings = []
rev_nam = []
final_name = []

for i in range(num):
    S = input()
    rev_nam.append(S)

for name in rev_nam:
    for i in range(len(name)):
        if i%2 == 0:
            even_strings.append(name[i])
        else:
            odd_strings.append(name[i])

        even_formatted_string = "".join(even_strings)
        odd_formatted_string = "".join(odd_strings)

        names = even_formatted_string + 2 * " " + odd_formatted_string
    print(names)

OUTPUT :输出 :

Hce  akr
HceRn  akrak

Required Output:所需输出:

Hce akr
Rn ak

I tried clearing the input after importing os module and using我尝试在导入 os 模块并使用后清除输入

os.system('cls') os.system('cls')

but it doesn't seem to work or maybe i don't know where to place it.但它似乎不起作用,或者我不知道把它放在哪里。 Any refrences任何参考

Your "odd" variable declaration is inside the else block and will only be accessed at runtime, if you don't get into the else block your variable wouldn't exist.您的“奇数”变量声明位于 else 块内,并且只能在运行时访问,如果您不进入 else 块,您的变量将不存在。 Give it some default value outside the else block.在 else 块之外给它一些默认值。

I found the answer to my question.我找到了我的问题的答案。 The reason both variables were added was due to declaration of strings as global variables.添加这两个变量的原因是将字符串声明为全局变量。 So to re-iterate over the strings i had to declare the variables in local-scope.所以要重新迭代字符串,我必须在本地范围内声明变量。

num = int(input())

for stdin in range(num):
    s = input()   
    new = ''
    odd = ''
    even = ''
    for name in range(len(s)):
        if name % 2 == 0:
            even += s[name]
        else: 
            odd += s[name]   
        new = (even +" " + odd)

    print(new)

After doing this the output prompted as soon as i entered the input.执行此操作后,一旦我输入输入,就会提示输出。

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

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