在python中,我在else后面有一个print语句:但是在运行cod之后,它似乎并没有实际打印它

2024-05-13 08:56:46 发布

您现在位置:Python中文网/ 问答频道 /正文

pin1 = int(input("Please set a pin: "))
print("Welcome to Satan's Soul Bank, Enter ya pin!")
attempt = int(input("Please enter your pin number first"))

if attempt == pin1:
    print("Select operation.")
    print("1.Deposit Souls")
    print("2.Withdraw Souls")
    print("3.Check Soul balance")
    choice = int(input("Enter choice(1/2/3):"))
elif attempt != pin1:
              for i in range(2):
                  attempt = int(input("Invalid Attempt Please enter your pin 
number again"))
else:
    print ("Card Swallowed Contact SATAN")

代码本身与else后面的print语句不同:它似乎无法识别它,只是错过了它,基本上,我需要它来打印,卡已吞咽后3次,但当我把它放入elif区,它只是打印它,每次我得到的引脚错误,所以有没有其他方法来绕过,导致打印卡已吞咽后3次


Tags: numberinputyourpinintprintenterplease
3条回答

如其他答案所述,elif块覆盖了if块未满足的条件。为了确保在else块中打印语句,可以使用flag变量,该变量将在最大错误尝试之前设置为false。达到最大尝试次数后,将flag设置为true。你知道吗

如果flag设置为true,则print卡被吞下。联系撒旦…'

你需要重组你的代码来得到你想要的。你知道吗

pin1 = int(input("Please set a pin: "))
print("Welcome to Satan's Soul Bank, Enter ya pin!")

correct_pin = False

for i in range(3):
    attempt = int(input("Please enter your pin number first"))
    if attempt == pin1:
        correct_pin = True
        break
    else:
        print("Invalid PIN. {} attempts remain.".format(2 - i))

if correct_pin:
    print("Select operation.")
    print("1.Deposit Souls")
    print("2.Withdraw Souls")
    print("3.Check Soul balance")
    choice = int(input("Enter choice(1/2/3):"))
else:
    print ("Card Swallowed Contact SATAN")

我们循环3次,如果用户得到正确的pin码就退出,只有在pin码正确的情况下才提供进一步的选项。你知道吗

在你的代码里

if attempt == pin1:
    ...
elif attempt != pin1:
   ...

由于其中一个条件始终保持不变(无论attempt是否等于pin1),程序将永远不会到达else部分。你知道吗

相关问题 更多 >