Reputation: 67
my code is if choice == "1" prints code further than I want it too. I cant use break as I want the user to be able to enter the correct password. There's some more code previously that shows an enter screen and includes the choice = input("choice: ")
This is what I have
# 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)
With this code, I hope that when the user enter 1, "requires password prints" however this is what I get.
choice: 1
Incorrect option
Requires Password
Here is your Keyword
------ haemoglobin
Now press enter for your definitions
How do I stop at requires password and allow the user to enter their password. Also the Incorrect option, further in the code is showing and I can't rid of it.
Upvotes: 0
Views: 196
Reputation: 2822
First, reading your explanation shows that a part at higher level is missing in your code sample; would you please provide more details?
Second, a remark on your code: when working on an item that returns elements, Python best way is to use for
.
With that in mind, your code:
fp = open('answers.txt')
while 1:
line = fp.readline()
if not line:
break
print (line)
becomes a much clearer and leaner
for line in open('answers.txt'): # this is an iterator
print line,
This is explained in the Python Tutorial.
Upvotes: 1
Reputation: 32511
I suspect further back in your code you should have used raw_input
instead of input
when getting choice
choice = raw_input("Choose an option").strip()
Upvotes: 2
Reputation: 1858
# 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")
What happens is your first if
executes, printing Requires Password
then your control passs over to the second if-elif
ladder. None matches, which is expected as choice
is already equal to "1"
.
Then the rest of the code, not being under any if/elif
statement executes no matter what. The presence of the if-elif
block doesn't affect the execution of this block.
So what you need to do is either put the code of the block below, under some if-elif
(whichever condition needs to be met), else what you can do is, put another condition before this block can be entered to check if your desired conditions have been met.
Upvotes: 1
Reputation: 39698
To answer your second question, you want to use a series of if/elif/else statements, as you've done later on, such as is included below.
if choice =='1':
#do stuff here
elif choice=='2':
#do stuff here
else:
print("Invalid choice")
As far as the first one goes, I would recommend calling a subroutine that allows for entering the password. Here's the start of such a subroutine:
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')
Upvotes: 0