PDA(容许年龄)算法?

2024-09-27 22:19:43 发布

您现在位置:Python中文网/ 问答频道 /正文

该项目的目的是创建一个简单的Python程序,它将提示用户输入其年龄,然后根据允许的年龄算法打印出用户日期的上下限。在

PDA算法是:d=a/2+7,a是您的年龄,d是您的日期允许的最低年龄,其中a是整数。在

以下是我目前掌握的代码:

import random
import sys
import time
def findACompanion():
     print "Welcome to the Permissible Dating Age Program!"
     sys.stdoutflush()
     time.sleep(3)
     a = float(raw_input("What is your age?"))
     if a <= 14:
          print "You are too young!"
     else: 
          d = a/2 + 7
          print "You can date someone"
          print d
          print "years old."

它看起来运行得很好,但是没有打印出来,我很困惑打印语句出了什么问题。在


Tags: 代码用户import程序目的算法youtime
2条回答

说实话,你没有那么离谱,但你的书面陈述没有错。相反,它们包含在一个函数中,而你从来没有调用过它们,所以它们永远不会实际运行。还有一个小错误。此代码将运行:

import random #Not needed with current code
import sys
import time

def findACompanion():
     print "Welcome to the Permissible Dating Age Program!"
     sys.stdout.flush() #You missed a full-stop
     time.sleep(3)
     a = float(raw_input("What is your age?"))
     if a <= 14:
          print "You are too young!"
     else: 
          d = a/2 + 7
          print "You can date someone"
          print d
          print "years old."

#Something to call your function and start it off
start_program  = findACompanion()

坚持上课,不会花很长时间的。被扔在最深处是最好的办法:)

{1>在函数中没有被定义,但是函数中没有被执行的语句。您可以从提示符中自己调用它:

>>> findACompanion()

Python中有一个常见的约定,用于检测是否将文件作为主程序运行,并自动进行调用,请参见Top-level script environment。约定要求函数被调用main,但您可以调用任何您想调用的函数。在

^{pr2}$

相关问题 更多 >

    热门问题