Python 我可以在导入模块之前运行打印语句吗?

Python 我可以在导入模块之前运行打印语句吗?,python,python-import,Python,Python Import,我是python和一般编码的初学者,我想知道如何在导入模块之前运行print语句。我正在做一个数字猜谜游戏,在我的主文件中,我将所有的模块组合在一起,我有一个通用的函数来运行所有的代码。最好是我展示我的代码,这样你们就能更好地理解我在处理什么: import random import lvl1 import time level1 = lvl1.Level_1_activated() # This is the main file combining everything together

我是python和一般编码的初学者,我想知道如何在导入模块之前运行print语句。我正在做一个数字猜谜游戏,在我的主文件中,我将所有的模块组合在一起,我有一个通用的函数来运行所有的代码。最好是我展示我的代码,这样你们就能更好地理解我在处理什么:

import random
import lvl1
import time

level1 = lvl1.Level_1_activated()

# This is the main file combining everything together to make this game playable

introduction = """
Hello and welcome to NumGuess by Sava. Here is a little bit about the game:
The game works by having 3 levels, each where you must pick a number between a range of
1-10 (level 1), 1-20 (level 2), and 1-50 (level 3).
You are given 5 attempts in the first level, 10 in the second level, and 20 in the final one.
You can also access a hint by typing ‘hint’. You win the game by picking the right number in each level.
You lose the game when you run out of tries. You can get a free bonus with 5 extra tries if you type ‘hlp’. 





"""
def start_game(time, lvl1):
    print(introduction)
    level1
    
    
start_game(time, lvl1)
这只是主模块的代码,我有lvl1的代码(这是我的“游戏”的第一级),我有一个类,它有所有的函数,然后参与while循环。我还将显示该文件:

import random
import time

# I will fist make variables for the time breaks. S is for short, M is for medium and L is for long

S = 0.2
M = 0.7
L = 1.1

class Level_1_activated():





    def get_name(self):

        # This function simply asks the name of the player

        name = input("Before we start, what is your name? ")

        time.sleep(S)
        print("You said your name was: " + name)

    def try_again(self):

        # This asks the player if they want to try again, and shows the progress of the level
        
        answer = (input("Do you want to try again? "))
        time.sleep(M)

        if answer == "yes":
            print("Alright!, well I am going to guess that you want to play again")
            time.sleep(M)

            print("You have used up: " + str(tries) + " Of your tries. Remember, when you use 5 tries without getting the correct number, the game ends")
            # Return statement for if the player wants to play again 
            return True

        else:
            print("Thank you for playing the game, I hope you have better luck next time")
            # This is the return statement that stops the while loop 
            return False


    def find_rand_num(self, random):

        # This is the core of the level, where the player just chooses numbers between 1 and 10
        time.sleep(S)   

        print("The computer is choosing a random number between 1 and 10... beep beep boop")
        time.sleep(L)

        # The list of numbers for the level that the player is on at the moment
        num_list = [1,10]
        number = random.choice(num_list)

        ques = (input("guess your number, since this is the first level you need to choose a number between 1 and 10  "))
        print(ques)

        if ques == str(number):
            time.sleep(S)
            print("Congratulations! You got the number correct!")

            # Yet another return statement for the while loop
            return "Found"
            
        elif input != number:

            time.sleep(M)
            print("Oops, you got the number wrong")


    # This variable is fairly self-explanatory; it is what controls how many itterations there are in the while loop 
tries = 1


while tries < 6:

    if tries < 2:
        Level_1_activated().get_name()
    
    res = Level_1_activated().find_rand_num(random)
    if res == "Found":
        break

    checker = Level_1_activated().try_again()
    if checker is False:
        break
    
    tries += 1

我故意将print语句放在模块之前,让它首先运行,我尝试了不同的方法,但似乎仍然无法理解我在这里做错了什么。感谢您花时间阅读代码,如果您有可能解决此问题,我将不胜感激。

您可以做很多事情,其中之一是将代码封装到只在您要求时运行的函数中

1级

\uuuu name\uuuuu
是一个特殊变量,如果直接执行,python将分配值
“\uuuuu main\uuuuu”
,否则它将是文件名,例如
“lvl1”

在你的主要工作中,你可以导入它并做类似的事情

import lvl1
...

def start_game():
    print(introduction)
    lvl1.run_game()

处于顶层的lvl1模块中的循环应该位于主模块调用的函数中,以便您可以在打印后调用它,而不是在导入模块时运行它。我已经尝试过这样做,但我不确定如何编写代码。抱歉,我还在学习python。仅仅编写
level1
完全没有效果(除了验证您是否确实有一个同名变量)。你必须对这个值做一些实际的处理——如果它是一个函数就调用它,如果它是一个类的实例就调用它的方法,等等。好的,我明白了,谢谢你的帮助。非常感谢!我需要删除指示尝试的print语句,但我可以将其放在循环中的任何位置。你值得投票!
... #all the previous code    

def run_game(): 
    tries = 1
    while tries < 6:
        ...
        tries += 1
if __name__ == "__main__":
    #if true it mean that you're executing this module directly, otherwise it was imported
    #and you include here whatever you want that happens when you execute the module directly but not when is imported, like for example running a game
    run_game()
    
import lvl1
...

def start_game():
    print(introduction)
    lvl1.run_game()