我有一个全局变量,需要通过函数生成的用户输入进行更改。
我正在尝试制作一个Zork 风格的文本游戏,并希望input
用户在创建角色期间更改角色名称function
来更改全局变量。
我已经能够创建一个来存储字符信息,并且能够在用户可以使用选项时出现的模拟命令提示符上class
显示大部分信息。class
input
在角色创建阶段之前,我使用 aglobal variable
来定义角色的名称。我使用 ' global
' 关键字来改变usercreation()
function
的 'name' 。variable
input
当提示准备好使用时,它仍然只显示名称00
而不是input
在creation()
function
我非常新手。任何建议、提示或方向都会受到重视。
import time
name = "00" ##this is what we want to change
##
def Intro():
print("\n\n\nWelcome to the game.")
time.sleep(1)
print("This is an attempt to make an interactive text based game.")
##
def Creation():
Character_name = input("\nWhat will your Character's name be: ")
time.sleep(1)
print("\nWelcome to the game " + Character_name + " \n" )
time.sleep(1.5)
Character_class = input("In one word, name " + Character_name + "'s profession: ")
t00n = Character_name + " the " + Character_class
global name ##here I am using the global keyword
name = t00n
time.sleep(1)
print("\n" + t00n + "\n")
time.sleep(2)
next_func = input("When ready type 'next' to begin.\n>>>:")
if next_func == "next":
segway()
else:
Jump()
##
def Jump():
Jump_Prompt = input("Just 'Jump' on in\n>>>: ")
if Jump_Prompt == "Jump":
segway1()
else:
Jump()
##
def segway():
print("A room with options to choose from")
prompt()
class Character:
def __init__(self, name, HP, full_HP, AtS, AR):
self.name = name ##should = t00n now?
self.hp = HP
self.full_HP = full_HP
self.AtS = AtS
self.AR = AR
def stat_bar(self):
return '{} {} {} {} {} {}'.format("[Name:]", self.name, "[HP:]", self.hp, "[Max HP:]", self.full_HP)
Player1 = Character(name, 100, 100, 1, 0)
##
def prompt():
_pr = input("<<< " + Character.stat_bar(Player1) + " >>> \n")
return _pr
#Begin
Intro()
Creation()
segway()
##The prompt() function still displays the name as 00 even tho the creation() function is using the 'global' keyword to change the 'name' variable to the user input.