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