这是我的代码,用于试图弄清访问18+网站的人的年龄(https://www.reddit.com/r/beginnerprojects/comments/3hrbpo/project_verifying_a_users_age/),我有两个问题。1)我不知道如何将列表网站添加到我的else字符串中。2)我想知道是否可以为currentDate导入当前年份,这样就不必手动将其输入。再次感谢您,感谢您的草率代码,我还是Python的新手!
import datetime
websites = ["www.youtube.com", "www.quora.com", "www.twitter.com"]
while True:
currentDate = 2015
print("WARNING: THIS WEBSITE IS FOR 18 YEARS OR UP")
userAge = int(raw_input("What is the year of your birth? "))
if userAge + 18 <= currentDate:
print "Welcome!"
break
elif userAge + 18 > currentDate:
print ("We're sorry, but this website isn't for you, try some of these websites: ")
continue
干得好:
elif
子句-else
处理“其他”条件(因为您elif
与您相反if
)代码:
from datetime import date
websites = ["www.youtube.com", "www.quora.com", "www.twitter.com"]
while True:
print("WARNING: THIS WEBSITE IS FOR 18 YEARS OR UP")
userAge = int(raw_input("What is the year of your birth? "))
if userAge + 18 <= date.today().year:
print "Welcome!"
break
else:
print ("We're sorry, but this website isn't for you, try some of these websites: {}".format(",".join(websites))
continue
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句