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")
每当我尝试运行代码时,它声明选择未定义,如果用户错误地将引脚错误3次,则代码基本上意味着吞下卡但是它停止声明选择未定义且用户确实获得它是正确的仍然说选择没有定义,我是否遗漏了一些额外的代码,如果是这样或是它的格式化方式?
答案 0 :(得分: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"))
choice = 0
if attempt == pin1:
# rest of your code here
如您所见,通过在任何条件语句之前放置choice
变量,确保它已创建并保持默认值。这有助于解决此特定问题。
答案 1 :(得分:0)
选择变量仅在 PIN 匹配时定义。
当 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"))
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')
最后三行是新的。