python - 添加列表和字符串并导入当前年份

标签 python list

这是我的代码,用于尝试找出访问 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

最佳答案

给你:

  1. 您希望 date.today() 获取当前日期
  2. 您想要使用 string.format() 和 string.join() 将列表连接在一起并将其内容插入到输出中
  3. 不需要 elif 子句 - else 处理“otherwise”条件(因为您的 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

关于python - 添加列表和字符串并导入当前年份,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33272851/

相关文章:

python - PEP 0492 - Python 3.5 异步关键字

python - 我可以在 python 中将 with 语句与列表一起使用吗?

C# 字符串排序...如何比较非序数但以相同字母开头的字符串将小写放在大写后面?

c# 字母数字列表排序通过检查列表项是否包含

python - 为什么在 Python 中使用 [e] * n(用于创建单个项目重复 n 次的列表)速度很快?

python - 启动通过套接字编程处理 HTTP 请求的 python 脚本时,CPU 达到 100%?

python - 无法打开使用 PyInstaller 创建的桌面应用程序

python - 时间间隔到均匀间隔的时间序列

list - 如何串联F#中的列表(和其他集合)?

class - Scala:基于类型过滤