例如,如果用户输入了“to”,我想知道他们实际上已经回答了"yes“。或者,如果他们写了"no“,我想知...
def ask_ok(prompt, retries=4, complaint='Yes or no, please!'): while True: ok = input(prompt) if ok in ('y', 'ye', 'yes'): return True if ok in ('n', 'no', 'nop', 'nope'): return False retries = retries - 1 if retries < 0: raise OSError('uncooperative user') pr...
def ask_ok(prompt, retries=4, complaint='Yes or no, please!'): pass 类似上面的函数,有2个默认参数,可以如下进行调用 ask_ok('Do you really want to quit?') ask_ok('OK to overwrite the file?',2) ask_ok('OK to overwrite the file?',2,'Come on, only yes or no!') 官网有给出2...
import random messages = ['It is certain', 'It is decidedly so', 'Yes definitely', 'Reply hazy try again', 'Ask again later', 'Concentrate and ask again', 'My reply is no', 'Outlook not so good', 'Very doubtful'] print(messages[random.randint(0, len(messages) - 1)]) 您可以...
那么和其他程序语言一样,Python也有很多流程控制工具,主要包括if条件语句、for循环语句以及函数等。 下面慢慢了解。 02 怎么办 条件if 和其他语言一样,如果很重要。可惜人生没有if。 >>> x = int(input("Please enter an integer: ")) Please enter an integer: 42 ...
def ask_ok(prompt,retries=4,complaint='Yes or no,please!'): while True: ok=input(prompt) if ok in ('y','ye','yes'): return True if ok in ('n','no','nop','nope'): return False retries=retries-1 if retries<0: raise OSError('uncooperative user') ...
def ask(prompt = "Do you like Python? ", hint = "yes or no"): while True: answer = input(prompt) if answer.lower() in ('y', 'yes'): print ("Thank you") return True if answer.lower() in ('n', 'no'): print("Why not ") return False else: print(hint) A. answer.lo...
def ask_ok(prompt, retries=4, complaint='Yes or no, please!'): while True: # True是关键字 ok = raw_input(prompt) #raw_input是内置的函数,用于IO输入 if ok in ('y', 'ye', 'yes'): return True if ok in ('n', 'no', 'nop', 'nope'): return False retries = retries - 1 ...
❻ print('What is your age?') # ask for their age myAge = input() print('You will be ' + str(int(myAge) + 1) + ' in a year.') myAge变量包含了input()函数返回的值。因为input()函数总是返回一个字符串(即使用户输入的是数字),所以你可以使用int(myAge)返回字符串的整型值。这个...
defask_ok(prompt,retries=4,complaint=Yesorno,please!): whileTrue: ok=raw_input(prompt) ifokin(y,ye,yes):return1 ifokin(n,no,nop,nope):return0 retries=retries-1 ifretries0:raiseIOError,refusenikuser printcomplaint 这个函数还可以用以下的方式调用:ask_ok(Doyoureallywantto quit?),或者像这样...