10 examples of 'how to ask a question in python' in Python

Every line of 'how to ask a question in python' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
27def ask_question(question, answers):
28 """
29 Asks a generic question.
30
31 Parameters
32 ----------
33 question: str
34 the text of the question.
35 answers: dict
36 the possible answers in the form of a dictionary like {value: text_in_radio_button}.
37
38 Returns
39 -------
40 int
41 the key of the choice in the answers dictionary.
42 """
43 root = Tk(baseName='QUESTION 1')
44
45 var = IntVar(value=0)
46 frm = Frame(root, bd=16)
47
48 T = Text(frm)
49 T.insert(END, question)
50 T.config(state=DISABLED)
51 T.pack()
52
53 frm.pack()
54 for answer in answers:
55 r = Radiobutton(frm, text=answers[answer], bd=4, width=12)
56 r.config(indicatoron=0, variable=var, value=answer)
57 r.pack(side='left')
58
59 def submit():
60 root.quit()
61 root.destroy()
62
63 Button(text='Submit', command=submit).pack(fill=X)
64 center(root)
65 root.mainloop()
66
67 return var.get()
6def ask(prompt: str='', default: str='') -> str:
7 """Print the ``prompt``, get user's answer, return it -- or a default."""
8 if prompt:
9 if default:
10 prompt = prompt + ' [' + default + ']'
11 prompt = prompt + ' '
12 answer = None
13 while not answer:
14 answer = input(prompt)
15 if default and not answer:
16 return default
17 return answer
10def query_yes_no(question, default="yes"):
11 """Ask a yes/no question via raw_input() and return their answer.
12
13 "question" is a string that is presented to the user.
14 "default" is the presumed answer if the user just hits .
15 It must be "yes" (the default), "no" or None (meaning
16 an answer is required of the user).
17
18 The "answer" return value is one of "yes" or "no".
19 """
20 valid = {"yes": True, "y": True, "ye": True,
21 "no": False, "n": False}
22 if default is None:
23 prompt = " [y/n] "
24 elif default == "yes":
25 prompt = " [Y/n] "
26 elif default == "no":
27 prompt = " [y/N] "
28 else:
29 raise ValueError("invalid default answer: '%s'" % default)
30
31 while True:
32 sys.stdout.write(question + prompt)
33 try:
34 choice = raw_input().lower()
35 except NameError:
36 choice = input().lower()
37 if default is not None and choice == '':
38 return valid[default]
39 elif choice in valid:
40 return valid[choice]
41 else:
42 sys.stdout.write("Please respond with 'yes' or 'no' "
43 "(or 'y' or 'n').\n")
115def AskForInput():
116 print("{:^40s}".format("You may enter 'hint' for a hint"))
117 print("{:^40s}".format("Please enter an alphabet: "))
7def ask_yn(question):
8 answer = ""
9 while True:
10 try:
11 answer = input(question + " [y/n]: ").lower()
12 if answer in ["y", "yes"]: return True
13 elif answer in ["n", "no"]: return False
14 except:
15 msg("")
16 continue
40def ask(msg, default=None, limits=None):
41 # answer = None
42 rng_msg = ""
43 if limits is not None:
44 (low, high) = limits
45 rng_msg = " [" + str(low) + "-" + str(high) + "]"
46 if default is not None:
47 msg += " (" + str(default) + ")"
48 msg += rng_msg + " "
49 if clint_present:
50 puts(text_colors[PROMPT](msg), newline=False)
51 else:
52 print(msg, end='')
53
54 return input()
139def ask(ask):
140 "Ask a Y/N question; defaults to *no* on errors"
141 try:
142 return query(ask) == "Y"
143 except:
144 return False
201def yes_no_prompt(question, default=None):
202 if default is None:
203 question += " [y/n] "
204 elif default == True:
205 question += " [Y/n] "
206 elif default == False:
207 question += " [y/N] "
208
209 while True:
210 print(question, end='')
211
212 choice = input().lower()
213 if choice in {'yes', 'y'}:
214 return True
215 elif choice in {'no', 'n'}:
216 return False
217 elif choice == '' and default is not None:
218 return default
126def ask_yes_no(prompt, default=None):
127 """Asks a question and returns a boolean (y/n) answer.
128
129 If default is given (one of 'y','n'), it is used if the user input is
130 empty. Otherwise the question is repeated until an answer is given.
131
132 An EOF is treated as the default answer. If there is no default, an
133 exception is raised to prevent infinite loops.
134
135 Valid answers are: y/yes/n/no (match is not case sensitive)."""
136
137 if default:
138 prompt = '%s [%s]' % (prompt, default)
139 return IPython.genutils.ask_yes_no(prompt, default)
263def _prompt_input(prompt):
264 if prompt is None:
265 return input(PROMPT)
266 else:
267 return input(prompt)

Related snippets