python - PDA (允许的约会年龄) 算法?

标签 python algorithm formula

该项目是创建一个简单的 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."

它似乎运行正常,但没有打印出来,我对打印语句出了什么问题感到困惑。

最佳答案

老实说,您并没有离题太远,但您的打印报表并没有错。相反,它们包含在您永远不会调用的函数中,因此它们永远不会真正运行。还有一个小错别字。此代码将运行:

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()

坚持上课,很快就会到位。被扔进深水区是最好的方式:)

关于python - PDA (允许的约会年龄) 算法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35041916/

相关文章:

crystal-reports - Crystal Reports 中的条件组 SUM

java - 使用循环和控制结构显示所有提出的奇数和偶数的总和

python - 在目录中的所有文件上运行 python 脚本

python - 如何在 Python 中使用 SSL 和身份验证连接到 SOAP 服务(没有 WSDL)?

python - python shelve.open(...,flag ='n' ) 不会创建空货架

algorithm - 损坏的计算器

algorithm - 分析时间复杂度

python - 为什么 Pandas 中的条形图不会堆叠不同的值?

检查每个位是否在某个周期上

excel - 嵌套 IF 以用零填充数字