社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
私信  •  关注

Philip DiSarro

Philip DiSarro 最近创建的主题
Philip DiSarro 最近回复了
5 年前
回复了 Philip DiSarro 创建的主题 » python测验,如何打印此测验/函数?

你会离开职能部门 q1 通过 q4 在外面 questions 函数,在问题函数中,您只需键入:

def questions():
    q1()
    q2()
    q3()
    q4()

然后在页面的底部 questions() 就像你已经在做的一样。整个程序看起来像:

def q1():
    print("What is 6 divided by 2?")
    answer = str(input())
    if answer == "3":
        print("You have {} lives left".format(lives))  # displays the current lives (does not lose a life)
        print("CORRECT!")
    else:
        lives -= 1  # loses a life
        print("You have {} lives left".format(lives))  # displays the current lives (loses a life)
        print("WRONG!")

def q2():
    print("What is 6 multiplied by 2?")
    answer = str(input())
    if answer == "12":
        print("You have {} lives left".format(lives))  # displays the current lives (does not lose a life)
        print("CORRECT!")
    else:
        lives -= 1  # loses a life
        print("You have {} lives left".format(lives))  # displays the current lives (loses a life)
        print("WRONG!")


def q3():
    print("What is 5 multiplied by 5?")
    answer = str(input())
    if answer == "12":
        print("You have {} lives left".format(lives))  # displays the current lives (does not lose a life)
        print("CORRECT!")
    else:
        lives -= 1  # loses a life
        print("You have {} lives left".format(lives))  # displays the current lives (loses a life)
        print("WRONG!")


def q4():
    print("What is 20 divided by 2?")
    answer = str(input())
    if answer == "12":
        print("You have {} lives left".format(lives))  # displays the current lives (does not lose a life)
        print("CORRECT!")
    else:
        lives -= 1  # loses a life
        print("You have {} lives left".format(lives))  # displays the current lives (loses a life)
        print("WRONG!")

def questions():
        q1()
        time.sleep(2)
        q2()
        time.sleep(2)
        q3()
        time.sleep(2)
        q4()

questions()
5 年前
回复了 Philip DiSarro 创建的主题 » 跳过python中的索引

最简单易读的方法是通过使用 while 而不是 for 循环如下所示。

li = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]
i = 0
while i < len(li):
    print(i)
    if i == 3:
        i = 10
        print(i)
    i += 1 # increment i at the end of the loop
5 年前
回复了 Philip DiSarro 创建的主题 » python:从函数返回异常
def test():
  ...
  if x.get(‘error’):
    raise

使用字典的内置功能,可以避免无意中引发错误。 get 功能。GET将返回 None 如果指定键处的值不存在,则不会引发异常。