我在从其他文件导入内容时遇到一些困难。我有以下文件,使用以下方法:
rest, go_hunting, gather_firewood, build_shelter, create_signal_fire, look_for_escape, try_to_start_fire
battle, win_battle, lose_battle
start_adventure, welcome_screen
gather_gear
create_player, formatter, die, return_to_camp, found_nothing, progress_bar, eat_food, restore
当你运行python game.py
游戏应该首先询问你的角色名称时,会发生什么。但是,当我运行它时,我得到以下结果:
Traceback (most recent call last):
File "game.py", line 1, in <module>
from choices import gather_fire_wood, go_hunting, build_shelter, \
File "C:\Users\thomas_j_perkins\bin\python\game\choices.py", line 1, in <modul
e>
from settings import formatter, BACKPACK, SURVIVAL_TOOLS, \
File "C:\Users\thomas_j_perkins\bin\python\game\settings.py", line 3, in <modu
le>
from choices import rest
ImportError: cannot import name rest
我尝试过的事情:
rest
移至设置并更改导入以反映该情况。rest
移至文件choices.py
print
放在任何地方现在有很多文件,而且我一直试图调试这个文件大约2个小时。
Choices.py:
from settings import formatter, BACKPACK, SURVIVAL_TOOLS, \
eat_food, return_to_camp, found_nothing, \
die, HEALTH
from random import randint, random
from battle import battle
def rest():
print formatter()
print "You sit down and feel kind of hungry."
foods = ['granola bars', 'cooked meat', 'dried foods', 'raw meat']
intersection = set(foods) & set(BACKPACK)
if intersection:
for food in BACKPACK:
print "{} of {}".format(BACKPACK[food], food)
while True:
choice = raw_input('What would you like to eat, enter \'nothing\' to return to camp: ').lower()
if choice in food:
eat_food(choice)
break
elif choice == 'nothing':
return_to_camp()
else:
print "Can't eat {}".format(choice)
def go_hunting():
animals = ['deer', 'elk', 'fox', 'rat', 'mountain lion', 'nothing'] # Random animals to choose from.
animal_came_across = random.choice(animals)
print "You start walking and come to a clearing." \
" You see something rustling in the bushes" \
" as you stare it jumps out." \
if animal_came_across == 'nothing':
print "The animal turns and runs away before you can" \
" get a good look at it.."
found_nothing()
else:
print "You have come across a {}!".format(animal_came_across)
print "You have the following to choose from:\n"
for weapon in BACKPACK:
print "{}".format(weapon)
print "\n"
while True:
weapon = raw_input('Choose your weapon: ')
if weapon not in BACKPACK:
print "Can't use that, you don't have it!"
else:
print 'Prepare to attack your prey!'
battle(animal_came_across, weapon, HEALTH)
def gather_fire_wood():
print "You walk out into the woods." \
" You start gathering some firewood."
wood_gathered = randint(2, 10)
SURVIVAL_TOOLS['fire wood'] = wood_gathered
print "You have gathered {} logs.".format(wood_gathered)
while True:
choice = raw_input('Would you like to build a fire [Y/N]: ').lower()
if choice == 'y':
try_to_start_fire(wood_gathered)
break
elif choice == 'n':
print "You decide to try to make a shelter."
build_shelter()
break
else:
print "Expected 'Y' or 'N' got {}, try again..".format(choice)
def build_shelter():
print "building shelter"
def create_signal_fire():
logs_needed = randint(5, 10)
logs_obtained = SURVIVAL_TOOLS['fire wood']
print "To build a signal fire you will need {} logs" \
" You currently have {}.".format(logs_needed, SURVIVAL_TOOLS['fire wood'])
if logs_obtained < logs_needed:
print "You don't have enough logs to start a signal fire."
while True:
choice = raw_input('Would you like to gather more [Y/N]: ').lower()
if choice == 'y':
gather_fire_wood()
elif choice == 'n':
return_to_camp()
else:
print "Expected 'Y' or 'N' got {}, try again..".format(choice)
else:
fire_type = 'signal fire'
try_to_start_fire(fire_type, logs_needed)
def look_for_escape():
print formatter()
print "You start walking back the way you think you came." \
" You walk for what seems like ever and come back to the" \
" spot that you just left."
options = ['Find fire wood', 'Cry', 'Find food', 'Build a shelter', 'Build a signal fire',
'Try to find your way back']
print "Options are:"
for i, opt in enumerate(options):
print "[{}] {}".format(i + 1, opt) # Print out your possible options at this point
option = False
print "\n"
while option is not True:
choice = raw_input('What would you like to do, enter a number: ')
if choice == '1':
option = True
gather_fire_wood()
elif choice == '2':
option = True
print die('You start crying loudly. It starts to snow, you have no shelter and freeze.')
elif choice == '3':
option = True
go_hunting()
elif choice == '4':
option = True
build_shelter()
elif choice == '5':
option = True
create_signal_fire()
elif choice == '6':
option = True
look_for_escape()
else:
option = False
print "Expected 1-{} got {}".format(len(options), choice)
def try_to_start_fire(kind_of_fire, logs_needed=5):
if kind_of_fire == 'signal fire':
if 'matches' not in BACKPACK or 'fire starter' not in BACKPACK:
print
settings.py:
import sys
import time
from choices import rest
BACKPACK = {} # What you brought
SURVIVAL_TOOLS = {} # What you'll find
HEALTH = 100
def create_player():
"""Create your character name"""
player_name = raw_input('Enter your characters name: ').capitalize()
return player_name
def formatter(width=50):
"""Formatting for to keep everything all pretty
:type width: Integer"""
return '*' * width
def die(why):
"""Kill off the program because you did something stupid
:type why: String"""
print formatter()
return "{}. You slowly fade into darkness. Game over.".format(why)
def return_to_camp():
print formatter()
print "test" # TODO: Finish this method
def found_nothing():
print formatter()
print "test" # TODO: Finish this method
def progress_bar():
"""Console based progress bar, nothing to fancy.
I wanted to do it in color, but couldn't figure out
how to do it on a Windows machine."""
for i in range(101):
time.sleep(0.1)
sys.stdout.write("\r[{0}] {1}%".format('#'*(i/10), i))
sys.stdout.flush()
def eat_food(food):
if food == 'cooked meat':
restore(HEALTH, food, 25)
elif food == 'granola bars':
restore(HEALTH, food, 5)
elif food == 'dried foods':
restore(HEALTH, food, 10)
else:
restore(HEALTH, food, -5)
def restore(health, food, amount):
if amount > 0:
health += amount
print "You eat some {} and it makes you feel a bit better.".format(food)
else:
health -= amount
print "You eat some {} and start puking".format(food)
rest()
其余文件位于要点here。有人请向我解释我做错了什么吗?如果我从正确的文件导入,我不明白为什么我会收到导入错误,这是一个我丢失的简单修复?