[英]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.