Python &引用;“如果选择”;当我希望程序停止时,正在调用程序中的其他代码

Python &引用;“如果选择”;当我希望程序停止时,正在调用程序中的其他代码,python,Python,我的代码是如果choice==“1”打印的代码也比我想要的要远。我不能使用break,因为我希望用户能够输入正确的密码。前面还有一些代码显示enter屏幕,其中包括choice=input(“choice:”) 这就是我所拥有的 # password if choice == "1": print ("Requires Password") #open the entire text file if choice == "password": fp = open('answ

我的代码是如果choice==“1”打印的代码也比我想要的要远。我不能使用break,因为我希望用户能够输入正确的密码。前面还有一些代码显示enter屏幕,其中包括choice=input(“choice:”)

这就是我所拥有的

# password
if choice == "1":
    print ("Requires Password")

#open the entire text file 
if choice == "password": 
    fp = open('answers.txt')
    while 1:
        line = fp.readline()
        if not line:
            break
        print (line)

 #quiz programming
 #generate a random keyword
elif choice == "2":
     input("You Ready? Press Enter Then")
print ('''

 Here is your Keyword''')
import random
with open('keywords.txt') as f:
       a = random.choice(list(f)).strip() #.strip cleans the line \n problem
       print ("    ")
       print ("------", a)
有了这段代码,我希望当用户输入1时,“需要密码打印”,但这就是我得到的

choice: 1
Incorrect option
Requires Password


     Here is your Keyword

------ haemoglobin

     Now press enter for your definitions

如何在需要密码时停止,并允许用户输入其密码。还有一个错误的选项,代码中的进一步显示,我无法消除它。

我怀疑代码中的进一步显示在获取
选项时,您应该使用
原始输入
而不是
输入

choice = raw_input("Choose an option").strip()

我怀疑在您的代码中,您在获取
选项时应该使用
原始输入
而不是
输入

choice = raw_input("Choose an option").strip()

要回答第二个问题,您需要使用一系列if/elif/else语句,正如稍后所做的那样,如下所示

if choice =='1':
   #do stuff here
elif choice=='2':
   #do stuff here
else:
   print("Invalid choice")
至于第一个,我建议调用允许输入密码的子例程。下面是这样一个子程序的开始:

def enterPassword():
   #Code to enter password here
   if password =='password':
      print 'You entered the right password!
      return True
   else:
      print 'Wrong password'
      return False

if choice =='1':
    if enterPassword():
        #Authenticated, do stuff that requires password
    else:
        print('Wrong password')

要回答第二个问题,您需要使用一系列if/elif/else语句,正如稍后所做的那样,如下所示

if choice =='1':
   #do stuff here
elif choice=='2':
   #do stuff here
else:
   print("Invalid choice")
至于第一个,我建议调用允许输入密码的子例程。下面是这样一个子程序的开始:

def enterPassword():
   #Code to enter password here
   if password =='password':
      print 'You entered the right password!
      return True
   else:
      print 'Wrong password'
      return False

if choice =='1':
    if enterPassword():
        #Authenticated, do stuff that requires password
    else:
        print('Wrong password')
如果执行第一个
,则打印
需要密码
,然后将控制权转移到第二个
如果执行elif
梯形图。无匹配项,因为
选项
已等于
“1”

然后,不管发生什么情况,不在任何
if/elif
语句下的其余代码都会执行。如果elif
块不影响该块的执行,则出现

因此,您需要做的是将块的代码放在下面的某个
下,如果elif
(需要满足的任何条件),否则您可以做的是,在输入该块之前放置另一个条件,以检查您所需的条件是否已满足

如果执行第一个
,则打印
需要密码
,然后将控制权转移到第二个
如果执行elif
梯形图。无匹配项,因为
选项
已等于
“1”

然后,不管发生什么情况,不在任何
if/elif
语句下的其余代码都会执行。如果elif
块不影响该块的执行,则出现


因此,您需要做的是将块的代码放在下面的某个
下,如果elif
(需要满足的任何条件),否则您可以做的是,在输入该块之前放置另一个条件,以检查您所需的条件是否已满足。

首先,阅读您的解释会发现代码示例中缺少更高级别的部分;请你提供更多细节好吗

第二,关于代码的备注:当处理返回元素的项时,Python最好的方法是使用
for

考虑到这一点,您的代码:

fp = open('answers.txt')
while 1:
    line = fp.readline()
    if not line:
        break
    print (line)
变得更加清晰和精简

for line in open('answers.txt'):  # this is an iterator
    print line,

这是解释。

首先,阅读您的解释会发现您的代码示例中缺少更高级别的部分;请你提供更多细节好吗

第二,关于代码的备注:当处理返回元素的项时,Python最好的方法是使用
for

考虑到这一点,您的代码:

fp = open('answers.txt')
while 1:
    line = fp.readline()
    if not line:
        break
    print (line)
变得更加清晰和精简

for line in open('answers.txt'):  # this is an iterator
    print line,

这已经解释清楚了。

您的缩进被破坏了。阅读一下缩进在Python中的含义。第一步通常是调试它。你是怎么做的?你的压痕破了。阅读一下缩进在Python中的含义。第一步通常是调试它。你是怎么做到的?