在运行代码时循环不起作用

发布于 2025-01-18 08:56:03 字数 901 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

转角预定愛 2025-01-25 08:56:03

以我的评论为基础。问题的直接原因是没有任何年份可以小于 1980 年且大于 2014 年。另外因为您有一个 while 循环,所以我认为您想要不断地请求一年,直到输入有效的年份

def chineseZodiac(year):
        
    signs = {
        0: 'Dragon',
        1: 'Snake',
        2: 'Horse',
        3: 'sheep',
        4: 'Monkey',
        5: 'Rooster',
        6: 'Dog',
        7: 'Pig',
        8: 'Rat',
        9: 'Ox',
        10: 'Tiger',
        11: 'Hare'
    }
    
    return signs[( year - 2000 ) % 12]

year = 0

in_range = lambda year: (1980 <= year <= 2014)
while not in_range(year):
    year = int(input("enter year:"))
    
    if in_range(year):
        print("your chinese zodiac is", chineseZodiac(year))

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

def chineseZodiac(year):
        
    signs = {
        0: 'Dragon',
        1: 'Snake',
        2: 'Horse',
        3: 'sheep',
        4: 'Monkey',
        5: 'Rooster',
        6: 'Dog',
        7: 'Pig',
        8: 'Rat',
        9: 'Ox',
        10: 'Tiger',
        11: 'Hare'
    }
    
    return signs[( year - 2000 ) % 12]

year = 0

in_range = lambda year: (1980 <= year <= 2014)
while not in_range(year):
    year = int(input("enter year:"))
    
    if in_range(year):
        print("your chinese zodiac is", chineseZodiac(year))
孤独陪着我 2025-01-25 08:56:03

您输入年份值,然后检查它是否在可接受的时间范围内。好的。但如果您使用 while 循环执行此操作,您将永远一次又一次地重新打印输出。

只需使用一次性 if 语句,如下所示:

if year >= 1980 and year <= 2014:
    print(f"your chinese zodiac is {chineseZodiac(year)}")

另请注意 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:

if year >= 1980 and year <= 2014:
    print(f"your chinese zodiac is {chineseZodiac(year)}")

Also note the nicer F-String-Formatting in the print statement.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文