为什么我的 python if 语句不起作用? [英] Why is my python if statement not working?

查看:52
本文介绍了为什么我的 python if 语句不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望有人能帮助我.当我运行下面的函数时,无论输入是什么,都会打印规则.我看不出我做错了什么.

I hope someone can help me. when I run the below function, no matter what the input, the rules are printed. I can't see what I've done wrong.

def check_rules():
    while True:
       request = input("\nWould you like to know the rules? (y/n) ")
       if request == "y" or "Y":
           print("""
1. Each player takes it in turn to roll a dice.
2. The player then turns over a card with the same
   number as the number rolled to see how many ladybirds
   there are (0-3).
3. The player keeps the card.
4. If a player rolls a number that is not on an unclaimed
   card, play continues to the next player.
5. Play continues until there are no more cards.
6. The player with the most number of ladybirds wins.""")
           break
        elif request == "n" or "N":
           break
        else:
           print("\nI'm sorry, I didn't understand that.")

推荐答案

您的 if 语句格式不正确:

Your if statement is not formed correctly:

def check_rules():
    while True:
       request = input("\nWould you like to know the rules? (y/n) ")
       if request in ["y","Y"]:
           print("""
1. Each player takes it in turn to roll a dice.
2. The player then turns over a card with the same
   number as the number rolled to see how many ladybirds
   there are (0-3).
3. The player keeps the card.
4. If a player rolls a number that is not on an unclaimed
   card, play continues to the next player.
5. Play continues until there are no more cards.
6. The player with the most number of ladybirds wins.""")
           break
        elif request in ["n","N"]:
           break
        else:
           print("\nI'm sorry, I didn't understand that.")

布尔表达式不能像if something == x or y,你必须像if something == x or something == y

Boolean expressions cannot be like if something == x or y, you must state them like if something == x or something == y

这篇关于为什么我的 python if 语句不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆