在python中导入函数

在python中导入函数,python,function,import,Python,Function,Import,我正在尝试创建一个菜单,用户可以在其中选择他/她想要运行的程序的哪一部分。当我导入函数时,计算机会自动运行它,而不是等待用户输入。我应该怎么做才能只在调用时运行函数?我的代码: import hangman menu = raw_input("""Welcome to Menu, please choose from the following options: 1. Hangman game 2. 3. 4. Exit """)

我正在尝试创建一个菜单,用户可以在其中选择他/她想要运行的程序的哪一部分。当我导入函数时,计算机会自动运行它,而不是等待用户输入。我应该怎么做才能只在调用时运行函数?我的代码:

    import hangman

    menu = raw_input("""Welcome to Menu, please choose from the following options: 
    1. Hangman game
    2.
    3. 
    4. Exit
    """)

    if menu == 1: 
        hangman()
    elif menu == 2:
        "Something"
    elif menu == 3:
        "Something"
    elif menu == 4:
        print "Goodbye"
    else:
        print "Sorry, invalid input" 
hangman.py的代码如下所示:

import random

words = ["monitor", "mouse", "cpu", "keyboard", "printer",]  

attempts = [] # Stores user input

randomWord = random.choice(words) # Computer randomly chooses the word

noChar = len(randomWord) # Reads number of characters in the word

print randomWord , noChar
print "Hello, Welcome to the game of Hangman. You have to guess the given word. The first word has", noChar, " letters."

def game():    
    guess = raw_input ("Please choose letter")
    attempts.append(guess) # Adds user input to the list
    print (attempts)

    if guess in randomWord: 
        print "You have guessed the letter" 
    else: 
        print "Please try again"


while True:
    game()
    chance = raw_input ("Have a guess")
    if chance == randomWord:
        print "Congratulations, you have won!"
        break

如果没有看到
hangman.py
,我会假设它直接包含运行hangman游戏的代码,而不是封装在函数中。如果是这样的话,您创建了一个模块,没有函数(还没有)

把代码包起来

def run_hangman():
    # Your existing code, indented by 4 spaces
    # ...
按如下方式导入:

from hangman import run_hangman
run_hangman()
def main():
    stuff = raw_input('Starting new game. Please enter stuff to do things')
    run_hangman(stuff)


def run_hangman(options):
    if options == 'SKIP':
        important_values = 5
        vales_set_by_user = 'Player 1'
    else:
        values_set_by_user = options

    rest_of_code()
最后调用如下函数:

from hangman import run_hangman
run_hangman()
def main():
    stuff = raw_input('Starting new game. Please enter stuff to do things')
    run_hangman(stuff)


def run_hangman(options):
    if options == 'SKIP':
        important_values = 5
        vales_set_by_user = 'Player 1'
    else:
        values_set_by_user = options

    rest_of_code()

这是开始菜单:

import hangman
option = raw_input('1) Start Normal\n2) Quick Start\n3) Default') # '\n' is a new line
if option == '1':
    hangman.main()
elif option == '2':
    hangman.run_hangman('SKIP')
elif option == '3':
    handman.run_hangman('Default User')
在你的刽子手代码里,你想对它进行调制。你应该有这样的东西:

from hangman import run_hangman
run_hangman()
def main():
    stuff = raw_input('Starting new game. Please enter stuff to do things')
    run_hangman(stuff)


def run_hangman(options):
    if options == 'SKIP':
        important_values = 5
        vales_set_by_user = 'Player 1'
    else:
        values_set_by_user = options

    rest_of_code()

相关:。并使用
hangman.hangman()
而不仅仅是
hangman()
。。。Python的
import
与C的
include
不同。它创建一个模块对象,该对象将您在该模块中定义的内容作为属性。如果您在
hangman.py
文件中有一个名为
hangman
的函数,您还可以使用
from hangman import hangman
将菜单转换为int(或将菜单与字符串进行比较),因为原始输入返回字符串。try:menu=int(menu)除了value错误:打印“这不是一个有效的选项!!”我添加了刽子手代码。问题可能是我已经在itNo中创建了一个函数,问题是您的
while True:
块不在函数中-这意味着在导入模块时它将立即执行。将其包装在一个函数
def run_hangman():
中,然后像我描述的那样导入该函数,您应该会很好。