简体   繁体   English

Python:无法从函数范围正确返回变量

[英]Python: Can't return variable from function-scope properly

I am trying to have the user input two names into variables. 我试图让用户输入两个名称到变量中。 These variables will be mixed with scramble-function. 这些变量将与加扰功能混合在一起。 Then I want to have them seperated again with the unscramble function but I want the unscramble function to recognise which of the two words was the first input and which the second input.. but I am stuck with scope and have no clue how to solve this correctly. 然后, 我想用unscramble函数再次将它们分开,但我想unscramble函数识别两个单词中的哪个是第一个输入,哪个是第二个输入..但是我对作用域感到困惑,不知道如何解决这个问题正确地。

I appreciate any solution! 我感谢任何解决方案!

a = raw_input("First word please?")
b = raw_input("Second word please?")

def scramble(word1, word2):

    if len(word1) > len(word2):
        a1 = word1
        a2 = word2
    else:
        a1 = word2
        a2 = word1

    maxlength = max( len(word1),len(word2) )
    magicWord = ""

    for x in range(0,maxlength):

        magicWord += a1[x]

        if x < len(a2)-1:
            magicWord += a2[x]

        elif x == len(a1)-1:
            fin = len(a2)-1
            magicWord += a2[fin]

        else:
            magicWord += "0"

    return magicWord

magicWord = scramble(a,b)
print "\nYour magic word is: \""+magicWord+"\"\n"

magicSize = len(magicWord)



def unscramble(magicWord):

    z = 0

    if z == magicSize:
        return False

    else:
        while unscramble(magicWord):

            word1 = ""
            word2 = ""

            if z % 2 == 1:
                if magicWord[z] == "0":
                    z+= 1
                else:
                    word2 += magicWord[z]
                    z += 1
            else:
                if magicWord[z] == "0":
                    z+= 1
                else:
                    word1 += magicWord[z]
                    z += 1

            return word1

if word1 > word2:
    print "First input: " + word1
    print "Second input: " + word2
else:
    print "First input: " + word2
    print "Second input: " + word1

After

 if len(word1) > len(word2):
     a1 = word1
     a2 = word2
 else:
     a1 = word2
     a2 = word1

the original order is lost. 原始订单丢失。 Without this it's quite easy to reconstruct the order, as your scramble function always puts the first letter of the first word first. 没有这个,重构顺序就很容易了,因为您的scramble功能总是将第一个单词的第一个字母放在首位。

Then change your unscramble function to return word1, word2 and you get the words back in the original order. 然后更改您的unscramble功能以返回单词word1, word2单词2,您将以原始顺序获得单词。

By the way, you should have a look at itertools.zip_longest() . 顺便说一句,您应该看看itertools.zip_longest() It would make your scramble function easier to read. 这将使您的scramble功能更易于阅读。 for char1, char2 in itertools.zip_longest(word1, word2, fillvalue='0') loops through all letters in both words and uses '0' for the missing letters in the shorter word. for char1, char2 in itertools.zip_longest(word1, word2, fillvalue='0')循环遍历两个单词中的所有字母,并对较短单词中的丢失字母使用'0'

You never got rid of a or b , so you can use them to know which word was first and which was second. 您永远不会摆脱ab ,因此您可以使用它们来知道哪个单词第一和第二。 There's no need to manually unscramble them. 无需手动对其进行解密。 I would use a tuple to store them after scramble , so instead of: 我将使用元组在scramble之后存储它们,所以代替:

magicWord = scramble(a,b)

I would just do: 我会做:

magicWord_tuple = (a, b, scramble(a,b))

Then you just pull out whichever word you want when you need it. 然后,您只需在需要时取出想要的任何单词即可。

firstword = magicWord_tuple[0]
secondword = magicWord_tuple[1]
magicword = magicWord_tuple[2]

I'm sorry, but your unscramble function cannot recover the words any more, let alone tell which one was entered first. 抱歉,您的解密功能无法再恢复单词,更不用说先输入哪个单词了。 You've destroyed information in your scramble function. 您已经破坏了加扰功能中的信息。

In scramble, your first few lines destroy the ordering; 在争夺中,您的前几行破坏了顺序。 since you never again refer to word1 and word2 (the only indicator of order), that information is lost. 因为您再也不会引用word1和word2(顺序的唯一指示符),所以该信息会丢失。 Perhaps you would want to use a different separator (where you insert a "0") to denote which word is which. 也许您想使用其他分隔符(在其中插入“ 0”)来表示哪个单词是哪个单词。 This insertion will let the unscramble routine differentiate. 这种插入将使解密程序有所区别。

if len(word1) > len(word2):
    a1, a2 = word1, word2
    sep = '1'
else:
    a1, a2 = word2, word1
    sep = '2'
...
   else:
        magicWord += sep

There are several other coding improvements we could make to be more "Pythonic", but we're doing functionality right now; 我们还可以进行其他一些编码改进,使其更像“ Pythonic”,但是我们现在正在做功能。 code review goes into another group. 代码审查进入另一组。

maybe you can have an additional variable saying you which word enters first in the magic word, and pass this variable to the unscramble function to do the separation of the words. 也许您可以有一个附加变量,告诉您哪个单词在魔术单词中第一个输入,然后将此变量传递给解密函数以分离单词。 You can change the scramble word like this: 您可以像这样更改密码:

if len(word1) > len(word2):
    flag = 1
    a1 = word1
    a2 = word2
else:
    flag = -1
    a1 = word2
    a2 = word1

... ...

return magicWord, flag

and the unscramble will take the flag to recover the correct order of the words. 且解密将带有flag以恢复单词的正确顺序。 This is the only solution, since when you create the magicword you loose the information about which is the first and which is the second word. 这是唯一的解决方案,因为当您创建魔术字时,您会丢失有关哪个是第一个字和哪个是第二个字的信息。 This means that your unscramble function cannot work without this information. 这意味着没有这些信息,您的解密功能将无法工作。

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

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