我试图让用户输入一个出生日期,然后在这些数字中添加个人整数。另外,如果任何这些数字的总和大于或等于10,则循环重复并且该过程再次为该值运行。这里是我到目前为止的代码关于与循环加法
if (sumYear >= 10):
sumYear2=0
for num in str(sumYear):
sumYear2 += int(num)
print(sumYear2)
这个工作,但是我认为这将是一个循环来实现更好。如果有某种方式,我不会使用像sumYear2
那样的东西,那样会很棒。请注意,我认为我不能使用sum()
函数。
谢谢你们的帮助。我有一个问题,但。我不知道为什么这个代码没有被评估,当我提供的月份为02,当天的日期为30
while True:
year=input("Please enter the year you were born: ")
month=input("Please enter the month you were born: ")
day=input("Please enter the day you were born: ")
if(int(month)==2 and int(day)<=29):
break
elif(int(month)==1 or 3 or 5 or 7 or 8 or 10 or 12 and int(day)<=31):
break
elif(int(month)==4 or 6 or 9 or 11 and int(day)<=30):
break
else:
print("Please enter a valid input")
您的编辑:一般来说,你应该[问一个新的问题](http://stackoverflow.com/questions/ask)如果它不是原来的一个子问题。 – jfs
指定是否使用Python 2.x或3.x.你的代码看起来像Python 3. – jfs