我正在尝试编写一个简单的程序,其中python计算你名字中的字符数,并判断你的名字有多酷。 试图学习if和elif的陈述...... 这就是我到目前为止所拥有的
P.S。我正在使用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()
答案 0 :(得分: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()
。