Python 打印时不显示字符串变量
所以,我和我的一个朋友正在用Python进行Percy Jackson文本冒险(我们都是书呆子),我试图打印“嗯(玩家名称),这是营地混血”,但结果是:“嗯,这是营地混血!”。有人知道我做错了什么吗?(问题点在底部)(请不要取笑我们的组名)Python 打印时不显示字符串变量,python,printing,format,Python,Printing,Format,所以,我和我的一个朋友正在用Python进行Percy Jackson文本冒险(我们都是书呆子),我试图打印“嗯(玩家名称),这是营地混血”,但结果是:“嗯,这是营地混血!”。有人知道我做错了什么吗?(问题点在底部)(请不要取笑我们的组名) 导入时间 随机输入 进口泡菜 导入操作系统 答案A=['A','A'] 答案B=['B','B'] 答案C=['C','C'] 是=['y','y'] 否=['n','n'] save=['save'] 房间=0 name=“” def菜单(): 打印(“F
导入时间
随机输入
进口泡菜
导入操作系统
答案A=['A','A']
答案B=['B','B']
答案C=['C','C']
是=['y','y']
否=['n','n']
save=['save']
房间=0
name=“”
def菜单():
打印(“FireFluxGames呈现…\n\n”)
睡眠时间(0.5)
印刷品(“珀西·杰克逊”)
睡眠时间(0.1)
打印(“--------------”)
睡眠时间(0.1)
打印(“文本冒险\n\n”)
睡眠时间(0.1)
打印(“选项:\n”)
打印(“A.新游戏\n”
“B.从上次保存继续\n”
“C.查看信用证\n”)
menuinput=输入(“>>>”)
如果在答案A中输入菜单:
开始()
elif menuinput在答案B中:
加载()
elif menuinput在答案C中:
学分()
def save():
将open('save.pckl','wb')作为f:
腌菜场(f室)
pickle.dump(名称,f)
def load():
将open('save.pckl','rb')作为f:
房间=酸洗负荷(f)
名称=pickle.load(f)
印刷品(房间)
def start():
print(“你在森林中醒来。你记不清什么了。\n”
您环顾四周,发现您靠近一个\n
拱门上刻着这样的文字:营地混血\n
他们。你往拱门里看,看到一个又高又瘦的\n
“头发乌黑的少年朝你走来,\n”
“看起来很担心,他问你叫什么……”
name=输入(“你叫什么名字?>>”)
印刷品(名称)
房间=1
保存()
ch1()
def CHBRoom1():
打印(“嗯{},这是营地混血!”。格式(名称))
菜单()
您正在使用CHBRoom1
中的全局对象名称
,以及start
方法中的本地对象
理想情况下,您应该传递变量,而不是使用全局变量。但是,您可以通过使用global
关键字在所有位置使用全局对象name
来实现上述功能
import time
import random
import pickle
import os
answer_A = ['A', 'a']
answer_B = ['B', 'b']
answer_C = ['C', 'c']
yes = ['y', 'Y']
no = ['n', 'N']
save = ['save']
room = 0
name = ""
def menu():
print("FireFluxGames presents...\n\n")
time.sleep(0.5)
print("Percy Jackson")
time.sleep(0.1)
print("-------------")
time.sleep(0.1)
print("Text Adventure\n\n")
time.sleep(0.1)
print("Options:\n")
print("A. New Game\n"
"B. Continue from last save\n"
"C. View Credits\n")
menuinput = input(">>> ")
if menuinput in answer_A:
start()
elif menuinput in answer_B:
load()
elif menuinput in answer_C:
credits()
def save():
global name
with open('save.pckl', 'wb') as f:
pickle.dump(room, f)
pickle.dump(name, f)
def load():
global name
with open('save.pckl', 'rb') as f:
room = pickle.load(f)
name = pickle.load(f)
print(room)
def start():
global name
print("You wake up in a forest. You don't remember much.\n"
"You look around, and discover that you are near an\n"
"archway, with the words: Camp Half-Blood etched into\n"
"them. You look into the archway, and see a tall, lanky\n"
"teenager with jet black hair walking towards you,\n"
"looking concerned. He asks you what your name is...")
name = input("What is your name? >>> ")
print(name)
room = 1
save()
CHBRoom1()
def CHBRoom1():
global name
print("<Percy> Well {}, this is Camp Half-Blood!".format(name))
menu()
导入时间
随机输入
进口泡菜
导入操作系统
答案A=['A','A']
答案B=['B','B']
答案C=['C','C']
是=['y','y']
否=['n','n']
save=['save']
房间=0
name=“”
def菜单():
打印(“FireFluxGames呈现…\n\n”)
睡眠时间(0.5)
印刷品(“珀西·杰克逊”)
睡眠时间(0.1)
打印(“--------------”)
睡眠时间(0.1)
打印(“文本冒险\n\n”)
睡眠时间(0.1)
打印(“选项:\n”)
打印(“A.新游戏\n”
“B.从上次保存继续\n”
“C.查看信用证\n”)
menuinput=输入(“>>>”)
如果在答案A中输入菜单:
开始()
elif menuinput在答案B中:
加载()
elif menuinput在答案C中:
学分()
def save():
全局名称
将open('save.pckl','wb')作为f:
腌菜场(f室)
pickle.dump(名称,f)
def load():
全局名称
将open('save.pckl','rb')作为f:
房间=酸洗负荷(f)
名称=pickle.load(f)
印刷品(房间)
def start():
全局名称
print(“你在森林中醒来。你记不清什么了。\n”
您环顾四周,发现您靠近一个\n
拱门上刻着这样的文字:营地混血\n
他们。你往拱门里看,看到一个又高又瘦的\n
“头发乌黑的少年朝你走来,\n”
“看起来很担心,他问你叫什么……”
name=输入(“你叫什么名字?>>”)
印刷品(名称)
房间=1
保存()
ch1()
def CHBRoom1():
全局名称
打印(“嗯{},这是营地混血!”。格式(名称))
菜单()
您正在使用CHBRoom1
中的全局对象名称
,以及start
方法中的本地对象
理想情况下,您应该传递变量,而不是使用全局变量。但是,您可以通过使用global
关键字在所有位置使用全局对象name
来实现上述功能
import time
import random
import pickle
import os
answer_A = ['A', 'a']
answer_B = ['B', 'b']
answer_C = ['C', 'c']
yes = ['y', 'Y']
no = ['n', 'N']
save = ['save']
room = 0
name = ""
def menu():
print("FireFluxGames presents...\n\n")
time.sleep(0.5)
print("Percy Jackson")
time.sleep(0.1)
print("-------------")
time.sleep(0.1)
print("Text Adventure\n\n")
time.sleep(0.1)
print("Options:\n")
print("A. New Game\n"
"B. Continue from last save\n"
"C. View Credits\n")
menuinput = input(">>> ")
if menuinput in answer_A:
start()
elif menuinput in answer_B:
load()
elif menuinput in answer_C:
credits()
def save():
global name
with open('save.pckl', 'wb') as f:
pickle.dump(room, f)
pickle.dump(name, f)
def load():
global name
with open('save.pckl', 'rb') as f:
room = pickle.load(f)
name = pickle.load(f)
print(room)
def start():
global name
print("You wake up in a forest. You don't remember much.\n"
"You look around, and discover that you are near an\n"
"archway, with the words: Camp Half-Blood etched into\n"
"them. You look into the archway, and see a tall, lanky\n"
"teenager with jet black hair walking towards you,\n"
"looking concerned. He asks you what your name is...")
name = input("What is your name? >>> ")
print(name)
room = 1
save()
CHBRoom1()
def CHBRoom1():
global name
print("<Percy> Well {}, this is Camp Half-Blood!".format(name))
menu()
导入时间
随机输入
进口泡菜
导入操作系统
答案A=['A','A']
答案B=['B','B']
答案C=['C','C']
是=['y','y']
否=['n','n']
save=['save']
房间=0
name=“”
def菜单():
打印(“FireFluxGames呈现…\n\n”)
睡眠时间(0.5)
印刷品(“珀西·杰克逊”)
睡眠时间(0.1)
打印(“--------------”)
睡眠时间(0.1)
打印(“文本冒险\n\n”)
睡眠时间(0.1)
打印(“选项:\n”)
打印(“A.新游戏\n”
“B.从上次保存继续\n”
“C.查看信用证\n”)
menuinput=输入(“>>>”)
如果在答案A中输入菜单:
开始()
elif menuinput在答案B中:
加载()
elif menuinput在答案C中:
学分()
def save():
全局名称
将open('save.pckl','wb')作为f:
腌菜场(f室)
pickle.dump(名称,f)
def load():
全局名称
将open('save.pckl','rb')作为f:
房间=酸洗负荷(f)
名称=pickle.load(f)
印刷品(房间)
def start():
全局名称
print(“你在森林中醒来。你记不清什么了。\n”
您环顾四周,发现您靠近一个\n
拱门上刻着这样的文字:营地混血\n
他们。你往拱门里看,看到一个又高又瘦的\n
“头发乌黑的少年朝你走来,\n”
“看起来很担心,他问你叫什么……”
name=输入(“你叫什么名字?>>”)
印刷品(名称)
房间=1
保存()
ch1()
def CHBRoom1():
全局名称
打印(“嗯{},这是营地混血!”。格式(名称))
菜单()
在start()
中,您没有更新全局变量名。而是创建一个新的本地
def start():
global name
print("You wake up in a forest. You don't remember much.\n"
"You look around, and discover that you are near an\n"
"archway, with the words: Camp Half-Blood etched into\n"
"them. You look into the archway, and see a tall, lanky\n"
"teenager with jet black hair walking towards you,\n"
"looking concerned. He asks you what your name is...")
name = input("What is your name? >>> ")
print(name)
room = 1
save()
CHBRoom1()
def start():
print("You wake up in a forest. You don't remember much.\n"
"You look around, and discover that you are near an\n"
"archway, with the words: Camp Half-Blood etched into\n"
"them. You look into the archway, and see a tall, lanky\n"
"teenager with jet black hair walking towards you,\n"
"looking concerned. He asks you what your name is...")
name = input("What is your name? >>> ")
print(name)
room = 1
save()
CHBRoom1(name)
def CHBRoom1(name):
print("<Percy> Well {}, this is Camp Half-Blood!".format(name))
def CHBRoom1(name):
print("<Percy> Well {}, this is Camp Half-Blood!".format(name))
CHBRoom1("Pablo")
#<Percy> Well Pablo, this is Camp Half-Blood!
def start():
print("You wake up in a forest. You don't remember much.\n"
"You look around, and discover that you are near an\n"
"archway, with the words: Camp Half-Blood etched into\n"
"them. You look into the archway, and see a tall, lanky\n"
"teenager with jet black hair walking towards you,\n"
"looking concerned. He asks you what your name is...")
player_name = input("What is your name? >>> ")
print(player_name)
room = 1
save()
CHBRoom1(player_name)
def CHBRoom1(name):
print("<Percy> Well {}, this is Camp Half-Blood!".format(name))