我有两个功能,第一个叫第二个。但是,当我突破第二个函数时,它会在第一个函数中显示if语句中的文本。我不明白,为什么第二个函数调用第一个?其次,我不明白为什么它会从if语句中执行代码,其中条件从未得到满足。
#! /usr/bin/env python
'''A sorting app where the user gets to choose
between options and the options are ranked by
likes in a file stored on a file'''
import sys
import random
import pickle
def intro():
greeting = '''\nWelcome to chooser where your voice gets to be heard
Press Enter to begin greatness
Press anything else to be immediately banned
>>'''
enter = raw_input(greeting).lower()
if enter == '':
main()
if enter == 'admin':
print 'Entering Admin menu\n'
admin()
else:
print '''\nDid you think I was kidding?!
You're gone!\n'''
sys.exit()
# Enters the main program if the user presses Enter or else it quits
def main():
count = 0
while True:
nav = '''Type Go to play
Type Q to quit
Type admin to go to admin
>>'''
start = raw_input(nav).lower()
if start == 'q':
print '\nThank you for playing\nBye!\n'
break
else:
print 'Any other key restarts the function'
def chooser():
pass
if __name__ == '__main__':
intro()
''' -----Questions-----
Why does this function when it expires run the intro function instead of just
running out of scope????'''
这是打印出终端的内容:
答案 0 :(得分:1)
基本上在部分:
if enter == '':
main()
if enter == 'admin':
print 'Entering Admin menu\n'
admin()
else:
print '''\nDid you think I was kidding?!
你有两个seperete语句,一个'if'和一个'if-else'跟随它。 第一个检查输入是否为'',这个条件在你的例子中成立,所以调用main()并且everythig很好。 当main()返回时,退出第一个语句并输入第二个语句,检查输入是否为admin(这是否为false),如果不是,则进行打印。
这里的逻辑是:
else部分与第一个if(仅到第二个)完全没有关系。你需要做的是用'elif'替换第二个'if',从而制作一个'if-elif-else'语句,所以逻辑将是: