如果python中有/ else问题

时间:2015-06-14 23:33:06

标签: python if-statement

好的,我会尽我所能解释这一点。

我正在尝试在python中创建一个if / else语句,它基本上会告诉用户他们是否根据原始输入放入了正确的年龄,但是我的if语句仍然出现错误。我可以得到一些帮助吗?

from datetime import datetime
now = datetime.now()

print '%s/%s/%s %s:%s:%s' % (now.month, now.day, now.year, now.hour,now.minute, now.second)
print "Welcome to the beginning of a Awesome Program created by yours truly."
print " It's time for me to get to know a little bit about you"

name = raw_input("What is your name?")
age = raw_input("How old are you?")

        if age == 1 and <= 100
            print "Ah, so your name is %s, and you're %s years old. " % (name, age)
                else:
                        print " Yeah right!"

2 个答案:

答案 0 :(得分:2)

将你的if语句更改为:

if age >=1 and age <=100:

两件事:

  1. if age >=1 and <=100缺少age <=100

  2. 您最后错过了:

答案 1 :(得分:1)

以下是格式化和更正的代码:

import datetime

now = datetime.datetime.now()
print '%s/%s/%s %s:%s:%s' % (now.month, now.day, now.year, now.hour,now.minute, now.second)
print "Welcome to the beginning of a Awesome Program created by yours truly."
print " It's time for me to get to know a little bit about you"

name = raw_input("What is your name?")
age = raw_input("How old are you?")

while int(age) < 1 or int(age) > 100:
    print " Yeah right!"
    age = raw_input("How old are you?")

print "Ah, so your name is %s, and you're %s years old. " % (name, age)

另外,仔细检查缩进和你的年龄逻辑。