Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
for attempt in range(5):
password = input("Password: ")
if password == "changeme":
print("Thou Shall Pass Into Mordor")
break
else:
print("Thou Shall Not Pass Into Mordor")
I need help so it displays the tries into password like this:
Password(1 attempts):
Your attempt variable holds the number of tries. Concatenate that with a print statement in your for loop.
Edit: Deleted Add 1, it is not necessary.
password_attempt = 0
for attempt in range(5):
password = input("Password: ")
if password == "changeme":
print("Thou Shall Pass Into Mordor")
break
else:
password_attempt += 1
print("Thou Shall Not Pass Into Mordor")
print(Password(%s attempt(s)) %password_attempt
I just made a little change to the code you posted to match yours needs.
for attempt in range(1, 5): # Change range to be 1 - 5.
password = input("Password (%d attempts): " % attempt) # Change input message showing attempt number.
if password == "changeme":
print("Thou Shall Pass Into Mordor")
break
else:
print("Thou Shall Not Pass Into Mordor")
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
def beauty():
print("Belle")
return "Beast"
def gaston():
if beauty() == "Beast":
print("No one’s as slick")
elif beauty() == None:
print("No one’s as quick")
if beauty() == "Belle":
print("What a guy")
else:
print("Belle")
gaston()
Can anyone please explain why the code above is printing 4 lines instead of 3?
When you are checking for if beauty() is equal to text, the function is being called. And you have multiple if/elif statements... So the unction is being called more than once. So I suggest calling the function once and storing that in a variable.
def beauty():
print("Belle")
return "Beast"
def gaston(butty):
if butty == "Beast":
print("No one’s as slick")
elif butty == None:
print("No one’s as quick")
if butty == "Belle":
print("What a guy")
else:
print("Belle")
butty = beauty()
gaston(butty)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
i have a short question, im trying to make a quiz program, however. i want a functions data to be shared across the program,
so how do i make so the ask function gen get the data from getQuestion?
import random
All_questions = ["whats obamas last name ","Riclug is snygg ","Are traps gay "]
questions_Right = ["care","no","no"]
points = 0
tries = 3
ListNumber = len(All_questions)
def getQuestion():
question_number = random.randint(0, ListNumber - 1)
right_anwser = questions_Right[question_number]
Question = All_questions[question_number]
def ask(Question,right_anwser):
print("The question is: ")
anwser = input(Question+": ").casefold()
if anwser == right_anwser:
print("yes,", right_anwser,"was right\n")
All_questions.remove(Question)
questions_Right.remove(right_anwser)
else:
print("Sorry, but the answer was", right_anwser,"\n")
while True:
if ListNumber == 0:
print("Game over")
break
else:
print(ListNumber)
getQuestion()
ask()
print(All_questions)
import random
All_questions = ["whats obamas last name ","Riclug is snygg ","Are traps gay "]
questions_Right = ["care","no","no"]
def getQuestion(All_questions, questions_Right):
question_number = random.randint(0, len(All_questions) - 1)
right_answer = questions_Right[question_number]
question = All_questions[question_number]
return question, right_answer # added this to return the data
def ask():
Question, right_answer = getQuestion(All_questions, questions_Right)
answer = input(f"The question is :\n{Question} :").casefold()
if answer == right_answer:
print(f"Yes, {right_answer} was right\n")
All_questions.remove(Question)
questions_Right.remove(right_answer)
else:
print(f"Sorry, but the answer was {right_answer}\n")
while True:
if len(All_questions) == 0:
print("Game over")
break
else:
ask()
1 - you can return the needed data by doing:
return question, right_answer
2 - since the questions list will change every round it is better to pass it to the getQuestion method instead of using the global one directly and use len(All_questions) to get the new length every round:
def getQuestion(All_questions, questions_Right)
3 - some advice :
use snake casing
get_question
# instead of :
getQuestion
don't use global variables inside functions:
A = 1
def add(a, b):
return a + b
add(A, 5)
# instead of
A = 1
def add_to_A(b):
return A + b
add_to_A(5):
format your code properly and make sure that you name variables correctly.
Just simply use return:
def getQuestion():
# ... your code
return (Question, rightAnswer)
#... your code
Question, rightAnswer = getQuestion()
And maybe try to use a dictionary, instead of an question and an answer-array. That would fit better.
I recommend you to do further reading and make some tutorials, before you go on with your quiz-game:
About functions:
https://www.programiz.com/python-programming/function
About dictionaries:
https://www.programiz.com/python-programming/dictionary
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I want to login system with dictionary.This code is working but if I write a different username on the login screen, the program gives an error.
database={'name': '1234', 'name2': '5678', 'name3': '9012'}
def log():
name = input('Enter username: ')
ask = input('Enter pin: ')
if ask in database[name]:
print ('Welcome', name)
else:
print ('Invalid code')
log()
You are querying database[name] without first checking that name is in database. Instead do:
database={'name': '1234', 'name2': '5678', 'name3': '9012'}
def log():
name = input('Enter username: ')
ask = input('Enter pin: ')
if name in database:
if ask == database[name]: # changed this to an 'equals' rather than 'in'
print ('Welcome', name)
else:
print ('Pin wrong!')
else:
print ('Username does not exist.')
log()
First you have to check if the username actually exists because otherwise you are getting an error because your trying to access an entry in the dict that does not exist.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I am making a program where the user inputs some text, and I wan't all the characters in that text that is not in the variable "alfabet" to be changed to "?". How can I do this? I also want a to do it in two functions, main and clean_text. My code looks like this now:
def clean_text():
for char in text:
if char in alfabeth:
continue
elif char not in alfabeth:
#don't know what to do here
#text[] = "?"
def main():
userInput = input("type in text: ")
text = list(userInput)
if__name__ == "__main__":
main()
clean_text(text)
This would be the basic code you need:
alfabeth = 'abcdefghijklmnopqrstuvwxyz'
text_in = list(input('What ever'))
for x in range(len(text_in)):
if text_in[x] in alfabeth:
continue
else:
text_in[x] = '?'
text_in = "".join(text_in)
print(text_in)
If you want you can define everything as one function or two or what ever you like.
Hope it helps
This is how it can look with functions. It worked for me.
alfabeth = 'abcdefghijklmnopqrstuvwxyz'
def clean_text(text_in, alfabeth):
for x in range(len(text_in)):
if text_in[x] in alfabeth:
continue
else:
text_in[x] = '?'
def main():
global UsrInput
UsrInput = list(input('type in text: '))
return UsrInput
if __name__ == "__main__":
main()
clean_text(UsrInput, alfabeth)
print("".join(UsrInput))
You have to return something to be able to assign that user_input to a variable to pass into clean_text(). Here is a working version of your project. This will handle multiple words separated by space as well.
def clean_text(word):
alphabeth = 'vshthstmpd'
for idx, item in enumerate(word):
if item == ' ':
pass
elif item not in alphabeth:
word[idx] = '?'
return ''.join(word)
def main():
user_input = input('Type in text: ')
text = list(user_input)
return text
if __name__ == '__main__':
text = main()
print(clean_text(text))
Type in text: vash the stampede
v?sh th? st?mp?d?
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
def getPhoneNumber():
"""Returns phone number if valid"""
validPhoneNumber = False
emptyString = ''
#prompt user for phone number
while not validPhoneNumber:
phoneNumber = input('\nEnter phone number with last four letters being alphabet'\
'(910-555-book):')
#check if valid form
if len(phoneNumber) != 12 or phoneNumber[3] != '-' or \
phoneNumber[7] != '-':
print('Invalid Entry - Must be of the form 910-555-book')
else:
validPhoneNumber = True
while validPhoneNumber:
if not phoneNumber[0:3].isdigit() or not phoneNumber[4:7].isdigit():
print('Non-digit entered before end')
validPhoneNumber = False
if not phoneNumber[8:12].isalpha():
print('Non-alphabet entered in last four characters')
validPhoneNumber = False
return phoneNumber
def displayPhoneNumber(phoneNumber):
"""Displays alphabet to phone number"""
translate = {'a':2, 'b':2, 'c':2, 'd':3, 'e':3, 'f':3, 'g':4, 'h':4, 'i':4,
'j':5, 'k':5, 'l':5, 'm':6, 'n':6, 'o':6, 'p':7, 'q':7, 'r':7,
's':7,'t':8,'u':8,'v':8,'w':9, 'x':9, 'y':9, 'z':9,'0':0,'1':1}
print(phoneNumber[0:8] + translate[phoneNumber[8]] + translate[phoneNumber[9]]\
+ translate[phoneNumber[10]] + translate[phoneNumber[11]])
#Main
print('This program will allow you to enter a phone number with the last four')
print('characters being in the alphabet and print out the corresponding numbers.')
terminate = False
while not terminate:
phoneNumber = getPhoneNumber()
displayPhoneNumber(phoneNumber)
#continue?
response = input('Enter another phone number? (y/n):')
if response == 'n':
terminate = True
Is there something wrong in getPhoneNumber? Because if there wasn't and it was passed to displayPhoneNumber, I would think that it would print. So my guess is it's in getPhoneNumber. This is my first time trying to use the isdigit and isalpha using slice notation, so maybe it's in there?
while validPhoneNumber:
if not phoneNumber[0:3].isdigit() or not phoneNumber[4:7].isdigit():
print('Non-digit entered before end')
validPhoneNumber = False
if not phoneNumber[8:12].isalpha():
print('Non-alphabet entered in last four characters')
validPhoneNumber = False
If phoneNumber is a valid phone number, validPhoneNumber will never be false, and so the while validPhoneNumber loop will run forever.