该项目是创建一个简单的 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()
坚持使用类,它不会花很长时间,直到它到位。被扔在深端是最好的方法:)
您已经定义了一个函数findACompanion
,但没有调用该函数,因此该函数中的任何语句都没有被执行。
>>> findACompanion()
Python 中有一个常见的约定来检测您是否将文件作为主程序运行并自动进行调用,请参阅Top-level script environment。该约定要求将函数调用为main
,但您可以调用任何您想要的函数。
if __name__ == "__main__":
findACompanion()
本站系公益性非盈利分享网址,本文来自用户投稿,不代表码文网立场,如若转载,请注明出处
评论列表(17条)