0

我正在尝试编写一个简单的程序,python 计算你名字中的字符数并决定你的名字有多酷。试图学习 if 和 elif 语句......这就是我到目前为止所拥有的

PS我正在使用python 2.7

def main():
    print"This program will tell who has a cooler name on a scale of 1-10"
    print""
    name=input("Enter your name: ")
    if len(name)<=4:
        print"on a scale of 1-10 your name is a 2! what a lame name..."
    elif (len(name)>4) and (len(name)<=7):
        print"That is a cool name you deserve a 8/10! Awesome!"
    elif len(name)>7:
        print"you are a 10/10, no one has a better name than you!"

main()
4

1 回答 1

2
n = len(name)  # it might be wise to store this

if n <= 4:     # as you have already
    ...
elif n <= 7:   # you already know n > 4 here; no need to test for that
    ...
else:          # you already know n > 7 here; this should be else
    ...

此外,由于您使用的是 Python 2.7,因此您想要raw_input()而不是input().

于 2013-09-09T23:49:40.367 回答