Python 2.7 使用%s以字符串形式打印时,将使用旧版本的全局变量
我是编程新手,这是我第一次尝试Python中的独立项目。我决定通过为一个班级项目创建一个简单的文本冒险来挑战自己,我遇到了一个小问题 在这个游戏中,玩家选择一个化身。我将这个化身存储为一个全局变量Python 2.7 使用%s以字符串形式打印时,将使用旧版本的全局变量,python-2.7,Python 2.7,我是编程新手,这是我第一次尝试Python中的独立项目。我决定通过为一个班级项目创建一个简单的文本冒险来挑战自己,我遇到了一个小问题 在这个游戏中,玩家选择一个化身。我将这个化身存储为一个全局变量Toon,因为它在整个游戏中都被引用以确定正确的响应 在引言中,有一段将向玩家介绍第一个场景。我通过code academy学到了我称之为“%method”的方法,玩家选择的化身应该在这一段中提到,但它使用了Toon的初始值,该值初始化为:“我做错了什么 我确保全局Toon变量被正确更新,因为我让游戏打
Toon
,因为它在整个游戏中都被引用以确定正确的响应
在引言中,有一段将向玩家介绍第一个场景。我通过code academy学到了我称之为“%method”的方法,玩家选择的化身应该在这一段中提到,但它使用了Toon
的初始值,该值初始化为:“我做错了什么
我确保全局Toon
变量被正确更新,因为我让游戏打印出Toon
的当前值,并且它是正确的
这里是我的代码的一个截断版本,相关的代码片段按照它们在我的代码中的顺序呈现
# global declaration of Toon, and preparing the introduction paragraph for text wrapping
Toon = ""
introduction = """ "Hello %s, this is the US Military. You have been selected in the most recent draft as a candidate to serve your country. Please report to your nearest deafting outpost for your pre-enlistment interview, ASAP." [...] "Hello %s, I am Commander Sheperd. Please have a seat." """ % (Toon, Toon)
introduction_paragraph = textwrap.wrap(introduction, width = 70)
#player chooses their avatar
choice = raw_input("Who do you choose?")
process(choice)
global Toon
if choice == "1":
print ""
print "Greetings player 1!"
print ""
Toon = "player 1"
sit1()
elif choice == "2":
print ""
print "Salutations, player 2!"
print ""
Toon = "player 2"
sit1()
elif choice == "3":
print ""
print "How do you do, player 3!"
print ""
Toon = "player 3"
sit1()
# introduction_paragraph is then supposed to be `print`ed with the avatar name. Instead, it `print`s only `""` However, if i have it `print` the current value for `Toon`, it spits out the intended value (I.E.: player 1, player 2, player 3)
def sit1():
for introLine in introduction_paragraph:
print introLine
print ""
print """ "So, how do you see yourself serving your country?" Commander Sheperd asks"""
#First Descision
for sit1choice in sit1Choices:
print ""
print sit1choice
print ""
choice = raw_input("What do you say?")
process(choice)
print choice
print Toon
#here's the code for the process(command) function, in case you're curious.
def process(command):
str(command)
command.lower()
if command == "help":
print "To make a decision, just enter the corresponding number next to your choice"
print "Type 'about' if you want to know who helped to make this project!"
input()
elif command == "about":
for person in credits:
print "%s : %s" % (person, credits[person])
elif len(command) >= 1:
return command
else:
print "%s is not a valid command" % command
input()
def input():
command = raw_input("What do you want to do?")
process(command)
return command
在设置Toon之前声明变量简介。因此,引言包含初始值,即
“
只需向下移动最后两行,直到Toon实际包含用户输入。将Toon=“player X”
的每个实例替换为:
Toon = "player X"
introduction = """ "Hello %s, this is the US Military. You have been selected in the most recent draft as a candidate to serve your country. Please report to your nearest deafting outpost for your pre-enlistment interview, ASAP." [...] "Hello %s, I am Commander Sheperd. Please have a seat." """ % (Toon, Toon)
introduction_paragraph = textwrap.wrap(introduction, width = 70)
或者找到其他常见的时间点,您可以将名称添加到简介中,可能只是在它打印之前?当您使用
%
操作符格式化简介
字符串时,您正在将卡通
的值与其当前值一起插入字符串中。如果以后更改Toon
变量的值,introduction
将不会更改以匹配它。如果要将名称的插入延迟到以后(当名称实际有意义时),则需要推迟格式设置。您仍然可以在顶部定义格式字符串
# start by defining the introduction string, with formatting codes, but no values yet
introduction = "Hello %s, this is the US Military..."
# later, assign Toon
Toon = "player 1"
# when you are ready to print the introduction, do the formatting:
print introduction % Toon
请注意,您可能希望使用较新样式的字符串格式,而不是带有
%
运算符的较旧样式。在哪里定义了sit1
?哦,我忘了添加它。很抱歉我把这两行移到了玩家选择头像之后,但是就在for
循环打印段落之前,但是没有任何改变。我去了!只需将这两行移到sit1()定义中即可。将这些行放在sit1()定义之外的任何地方意味着该段落是在更新toon
之前打印的。现在是凌晨4点。难怪我犯了这么简单的错误,代码学院课程中顺便提到了str.format方法。这门课没有讲。正因为如此,我坚持使用旧方法,直到我掌握了基本知识。
# start by defining the introduction string, with formatting codes, but no values yet
introduction = "Hello %s, this is the US Military..."
# later, assign Toon
Toon = "player 1"
# when you are ready to print the introduction, do the formatting:
print introduction % Toon