简体   繁体   English

如何在python中存储字符串变量并在函数中传递它们?

[英]How do to store variables which are strings in python and pass them around in functions?

I have a problem where i get an error message telling me its used before its assigned.我有一个问题,我收到一条错误消息,告诉我它在分配之前已使用。 I am not sure how to pass around the variable result and answer in my code.我不确定如何在我的代码中传递变量结果和答案。 The problem is that i am creating a shift cypher.问题是我正在创建一个移位密码。

These are the instructions:这些是说明:

Complete the encode method so that it returns the encoded message.完成 encode 方法,使其返回编码的消息。 Complete the decode method so that it returns the decoded message.完成 decode 方法,使其返回解码后的消息。 In the main method, - prompt the user for the message.在 main 方法中, - 提示用户输入消息。 - prompt the user for the shift amount. - 提示用户输入偏移量。
- Print the message. - 打印消息。
- Call encode with the message and the shift amount. - 使用消息和移位量调用编码。
- Store the encoded message returned from encode. - 存储从 encode 返回的编码消息。
- Print the encoded message. - 打印编码的消息。
- Send the encoded message and the shift amount to the decode method. - 将编码的消息和移位量发送到解码方法。
- Print the decoded message. - 打印解码的消息。

def encode(s, shiftamount):
  for x in range(0, len(s)):

    if s[x] == " ":
      print(" ", end="")

    if s[x] in "ABCDEFGHIJKLMNOPQRSTUVWXYZ":
      if chr(ord(s[x]) + shiftamount) > 'Z' : 
        result += chr(ord(s[x]) + (shiftamount - 26)) 

      if chr(ord(s[x]) + shiftamount) <= 'Z' :
        result += chr(ord(s[x]) + shiftamount)

    if s[x] in "abcdefghijklmnopqrstuvwxyz":
      if chr(ord(s[x]) + shiftamount) > 'z' : 
        result += chr(ord(s[x]) + (shiftamount - 26)) 

      if chr(ord( s[x] ) + shiftamount) <= 'z' :
        result += chr(ord(s[x]) + shiftamount)
  return result

def decode(s, shiftamount):

  for y in range(0, len(s)):

    if s[y] == " ":
      print(" ", end="")

    if s[y] in "ABCDEFGHIJKLMNOPQRSTUVWXYZ":
      if chr(ord(s[y]) + shiftamount) > 'Z' : 
        answer += chr(ord(s[y]) - (shiftamount - 26)) 

      if chr(ord(s[y]) + shiftamount) <= 'Z' :
        answer += chr(ord(s[y]) - shiftamount)

    if s[y] in "abcdefghijklmnopqrstuvwxyz":
      if chr(ord(s[y]) + shiftamount) > 'z' : 
        answer += chr(ord(s[y]) - (shiftamount - 26)) 

      if chr(ord( s[y] ) + shiftamount) <= 'z' :
        answer += chr(ord(s[y]) - shiftamount)
  return answer


def main():
  s = input("Enter the message:")
  shiftamount = int(input("Enter the shift:"))
  print(s)
  encode(s,shiftamount)
  decode(s,shiftamount)
  print(result)
  print(answer)


########################################################################
###                Do not modify anything below here                 ###
########################################################################
if __name__ == '__main__':
      main()

You need to define the result and answer variables before using += operation, also in main you need to assign it to a local variable before printing.您需要在使用 += 操作之前定义 result 和 answer 变量,并且在 main 中您需要在打印之前将其分配给局部变量。

Lines: 52,53,25,2行数:52、53、25、2

def encode(s, shiftamount):
  result=''
  for x in range(0, len(s)):

    if s[x] == " ":
      print(" ", end="")

    if s[x] in "ABCDEFGHIJKLMNOPQRSTUVWXYZ":
      if chr(ord(s[x]) + shiftamount) > 'Z' : 
        result += chr(ord(s[x]) + (shiftamount - 26)) 

      if chr(ord(s[x]) + shiftamount) <= 'Z' :
        result += chr(ord(s[x]) + shiftamount)

    if s[x] in "abcdefghijklmnopqrstuvwxyz":
      if chr(ord(s[x]) + shiftamount) > 'z' : 
        result += chr(ord(s[x]) + (shiftamount - 26)) 

      if chr(ord( s[x] ) + shiftamount) <= 'z' :
        result += chr(ord(s[x]) + shiftamount)
  return result

def decode(s, shiftamount):

  answer=''

  for y in range(0, len(s)):

    if s[y] == " ":
      print(" ", end="")

    if s[y] in "ABCDEFGHIJKLMNOPQRSTUVWXYZ":
      if chr(ord(s[y]) + shiftamount) > 'Z' : 
        answer += chr(ord(s[y]) - (shiftamount - 26)) 

      if chr(ord(s[y]) + shiftamount) <= 'Z' :
        answer += chr(ord(s[y]) - shiftamount)

    if s[y] in "abcdefghijklmnopqrstuvwxyz":
      if chr(ord(s[y]) + shiftamount) > 'z' : 
        answer += chr(ord(s[y]) - (shiftamount - 26)) 

      if chr(ord( s[y] ) + shiftamount) <= 'z' :
        answer += chr(ord(s[y]) - shiftamount)
  return answer


def main():
  s = input("Enter the message:")
  shiftamount = int(input("Enter the shift:"))
  print(s)
  result=encode(s,shiftamount)
  answer=decode(s,shiftamount)
  print(result)
  print(answer)


########################################################################
###                Do not modify anything below here                 ###
########################################################################
if __name__ == '__main__':
      main()

try this:尝试这个:

def main():
  s = input("Enter the message:")
  shiftamount = int(input("Enter the shift:"))
  print(s)
  e = encode(s,shiftamount)
  d = decode(s,shiftamount)
  print(e)
  print(d)

you need to create reference, and instantiate it您需要创建引用并实例化它

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

相关问题 如何使字符串和变量可编辑并打印它们? (Python) - How do I make strings and variables editable and printing them? (Python) 如何在Python中将变量传递给Shell函数? - How do I pass variables to shell functions in Python? Python函数:如果仅访问全局变量,则传递全局变量? - Python functions: Pass global variables if only accessing them? 如何在Python和RobotFramework之间传递在Python函数内部创建的变量 - How to pass variables between Python and RobotFramework which are created inside Python functions 如何在python中将函数存储为类变量? - How to store functions as class variables in python? 如何在 class 中的函数之间传递变量,同时将它们定义为 object? - how to pass variables between functions in a class, while defining them as an object? 如何跨函数传递变量? - How do I pass variables across functions? 如何在python 3中的函数上传递变量 - How to pass variables over functions in python 3 为什么在将lambda函数赋值给变量时需要括号? - Why do I need brackets around lambda functions when assigning them to variables? 如何允许Python从2个文本文件(同一行)中选择一条随机行,然后将其存储为变量? - How do I allow Python to choose a random line from 2 text files (that are the same line) and then store them as variables?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM