在运行代码时循环不起作用
def chineseZodiac(year):
if (year - 2000) % 12 == 0:
sign = 'Dragon'
elif (year - 2000) % 12 == 1:
sign = 'Snake'
elif (year - 2000) % 12 == 2:
sign = 'Horse'
elif (year - 2000) % 12 == 3:
sign = 'sheep'
elif (year - 2000) % 12 == 4:
sign = 'Monkey'
elif (year - 2000) % 12 == 5:
sign = 'Rooster'
elif (year - 2000) % 12 == 6:
sign = 'Dog'
elif (year - 2000) % 12 == 7:
sign = 'Pig'
elif (year - 2000) % 12 == 8:
sign = 'Rat'
elif (year - 2000) % 12 == 9:
sign = 'Ox'
elif (year - 2000) % 12 == 10:
sign = 'Tiger'
else:
sign = 'Hare'
return sign
year = int(input("enter year:"))
while (year <= 1980 and year >= 2014):
print("your chinese zodiac is ", chineseZodiac(year))
``没有错误,但是在运行代码时不会显示while循环下的打印... 请帮助我,我是菜鸟:-('
def chineseZodiac(year):
if (year - 2000) % 12 == 0:
sign = 'Dragon'
elif (year - 2000) % 12 == 1:
sign = 'Snake'
elif (year - 2000) % 12 == 2:
sign = 'Horse'
elif (year - 2000) % 12 == 3:
sign = 'sheep'
elif (year - 2000) % 12 == 4:
sign = 'Monkey'
elif (year - 2000) % 12 == 5:
sign = 'Rooster'
elif (year - 2000) % 12 == 6:
sign = 'Dog'
elif (year - 2000) % 12 == 7:
sign = 'Pig'
elif (year - 2000) % 12 == 8:
sign = 'Rat'
elif (year - 2000) % 12 == 9:
sign = 'Ox'
elif (year - 2000) % 12 == 10:
sign = 'Tiger'
else:
sign = 'Hare'
return sign
year = int(input("enter year:"))
while (year <= 1980 and year >= 2014):
print("your chinese zodiac is ", chineseZodiac(year))
'there is no error but the print under the while loop doesn't show when you run the code...
please help me, im noob :-('
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
以我的评论为基础。问题的直接原因是没有任何年份可以小于 1980 年且大于 2014 年。另外因为您有一个 while 循环,所以我认为您想要不断地请求一年,直到输入有效的年份
Building on my comment. The immediate cause of your problem is there's no year that can be less than 1980 and greater than 2014. Also because you have a while loop, so I think you want to continually ask for a year until a valid year is entered
您输入年份值,然后检查它是否在可接受的时间范围内。好的。但如果您使用 while 循环执行此操作,您将永远一次又一次地重新打印输出。
只需使用一次性 if 语句,如下所示:
另请注意 print 语句中更好的 F-String-Formatting。
You input a value for year and then check if it is within the accepted timespan. ok. But if you do this with a while-loop, you will just reprint your output again and again for eternity.
Just use a one time if-statement like so:
Also note the nicer F-String-Formatting in the print statement.