Python 从根本上说,选择是没有定义的

Python 从根本上说,选择是没有定义的,python,Python,每当我试着运行代码时,它都会说选择没有定义,如果用户的pin错误了3次,代码基本上就是要吞下卡,但是它会停止说选择没有定义,如果用户做对了,它仍然说选择没有定义,我是否丢失了一些额外的代码,如果是这样的话,它的格式化方式是这样的?从您的代码中抽取一个小样本,您只需执行以下操作: pin1 = int(input("Please set a pin: ")) print("Welcome to Satan's Soul Bank, Enter ya pin!") attempt = int(inp

每当我试着运行代码时,它都会说选择没有定义,如果用户的pin错误了3次,代码基本上就是要吞下卡,但是它会停止说选择没有定义,如果用户做对了,它仍然说选择没有定义,我是否丢失了一些额外的代码,如果是这样的话,它的格式化方式是这样的?

从您的代码中抽取一个小样本,您只需执行以下操作:

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): "))
if choice == 1:
    Deposit = int(input("How many Souls would you like to deposit?: "))
    print ('You now have' , Deposit + 1042, 'Souls')
if choice == 2:
    Withdraw = int(input("How many Souls would you like to withdraw?: "))
if Withdraw < 1042:
     print ('You have withdrawn' , Withdraw, 'Souls, and now have' , Withdraw -1042, 'Souls left')
if Withdraw > 1042:
     print ('You do not have enough souls in your account to Withdraw that much')
elif choice == 3:
    print ("You have 1042 souls in your bank account")
elif attempt != pin1:
            for i in range(2):
                    attempt = int(input("Invalid Attempt Please enter your pin number again"))
            print ("Card Swallowed Contact SATAN")
正如您所看到的,通过将choice变量放在任何条件语句之前,可以确保它已创建并将保留默认值。这有助于解决此特定问题。

仅当PIN匹配时才定义choice变量

当PIN不匹配时,需要有一个else子句来阻止代码进一步运行到代码的安全部分:

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"))
choice = 0
if attempt == pin1:
    # rest of your code here

最后三行是新的。

是的,因为只要输入不等于pin1,代码就不会进入第一个条件,这是您首先引入choice的地方。相反,您应该在条件语句之前初始化此选项。类似choice=0的东西。介意把它放在代码中给我看吗?好的,当你在这里的时候,你也可以验证代码,并在修复代码后帮助我进行修改。多亏了你的代码,我面临的问题是,如果这是我提供的更正的具体撤回部分,这就解决了你所问的问题。我建议您回到您的代码,并仔细遵循它,看看还有什么不起作用。如果你在其他事情上仍然有困难,那么确保你仔细阅读了如何组合一个新问题,并张贴一个新问题,清楚地解释你遇到的新问题。但是请确保您阅读了我提供的链接,以便您的问题能够受到读者的欢迎。
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")
else:
    print("The PIN did not match.  Exiting")
    raise RuntimeError('Security:  PIN did not match')