我可以在导入的模块之前打印语句 运行 吗?
Can I make a print statement run before a module I imported?
我是 python 和一般编码的初学者,我想知道如何在导入模块之前制作打印语句 运行。我正在制作一个数字猜谜游戏,在我组合所有模块的主文件中,我有一个通用函数 运行 将所有代码组合在一起。如果我展示我的代码,那将是最好的,这样你们可以更好地理解我正在处理的事情:
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 的代码(这是我的 'game' 的第一级),我有一个 class 具有所有功能然后参与 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
如果你回到主文件中的这个函数:
def start_game(time, lvl1):
print(introduction)
level1
我故意将打印语句放在模块之前,使其成为 运行 首先,我已经尝试了不同的方法,但似乎仍然无法理解我在这里做错了什么.感谢您花时间阅读代码,如果你们中有人对此有可能的解决方案,我将不胜感激。
您可以做很多事情,其中之一是将您的代码封装到只有 运行 需要的函数中
lvl1
... #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()
__name__
是一个特殊变量,直接执行python会赋值为"__main__"
,否则就是文件名,如"lvl1"
例子
并且在您的 main 中,您可以导入它并执行类似
的操作
import lvl1
...
def start_game():
print(introduction)
lvl1.run_game()
我是 python 和一般编码的初学者,我想知道如何在导入模块之前制作打印语句 运行。我正在制作一个数字猜谜游戏,在我组合所有模块的主文件中,我有一个通用函数 运行 将所有代码组合在一起。如果我展示我的代码,那将是最好的,这样你们可以更好地理解我正在处理的事情:
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 的代码(这是我的 'game' 的第一级),我有一个 class 具有所有功能然后参与 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
如果你回到主文件中的这个函数:
def start_game(time, lvl1):
print(introduction)
level1
我故意将打印语句放在模块之前,使其成为 运行 首先,我已经尝试了不同的方法,但似乎仍然无法理解我在这里做错了什么.感谢您花时间阅读代码,如果你们中有人对此有可能的解决方案,我将不胜感激。
您可以做很多事情,其中之一是将您的代码封装到只有 运行 需要的函数中
lvl1
... #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()
__name__
是一个特殊变量,直接执行python会赋值为"__main__"
,否则就是文件名,如"lvl1"
例子
并且在您的 main 中,您可以导入它并执行类似
的操作import lvl1
...
def start_game():
print(introduction)
lvl1.run_game()