2015-08-11 3 views
-1

Я пытаюсь написать общую приключенческую игру для игры в Python, но у меня возникают проблемы с повторным использованием переменной. У меня будут другие функции, которые будут называть персонажа по имени, и я не совсем уверен, как заставить его работать. Я попытался сделать «имя» глобальной переменной, но ошибки. Вот мой код до сих пор:Ошибка при повторном использовании переменной для нескольких независимых функций

def name_grab(): 
    name = raw_input("What is your name? ") 
    print name 

name_grab() 

# it begins! 

def start_quest(name): 
    print name 
    print "You find yourself washed up on a beach." 
    print "Your ship was sunk by mercenaries during a violent storm on the ocean." 
    print "You are coverred in seaweed and you keep coughing up salty ocean water." 
    print "All you seem to have on you is your gun which was ruined by the water." 
    print "You need to find a new weapon and find out where you are." 

start_quest() 
+0

'start_quest (name_grab())'? 'name_grab' нужно будет * возвращать *' имя'. – vaultah

ответ

1
def name_grab(): 
    name = raw_input("What is your name? ") 
    return name 

# it begins! 

def start_quest(name): 
    print name 
    print "You find yourself washed up on a beach." 
    print "Your ship was sunk by mercenaries during a violent storm on the ocean." 
    print "You are coverred in seaweed and you keep coughing up salty ocean water." 
    print "All you seem to have on you is your gun which was ruined by the water." 
    print "You need to find a new weapon and find out where you are." 

name = name_grab() 

start_quest(name) 
+0

Если я создам еще одну функцию позже, хотя я не хочу, чтобы она снова запрашивала имя символа –

+0

Модифицировали ответ, чтобы сохранить имя в глобальной переменной 'name'. – Alex

+0

@GrillerGrog это работало? – Alex

Смежные вопросы