import time
#Initializing Variables
currentMoney = 0
depositedMoney = 0
takenMoney = 0
#Main Fucntion which shows when the program starts
def Main():
while True:
userChoice = input("Welcome to the ATM Organizer. To Preceed Enter 1 To Close Enter 0")
if userChoice == 1:
Atm()
elif userChoice == 0:
print("Thank you.Good Bye!")
break
def Atm():
Handling Invalid Inputs
while True:
try:
atm = int(input("Welcome Inside The ATM , To See your money , Type '1' , To put money to the cash machine , Type '2' , To take money out of the ATM , Type '3' , To Exit the ATM , Type '0' ."))
except ValueError:
print("You didn't choose what was given!")
continue
Input Choices
if (atm == 0):
Main()
elif (atm == 1):
print("You Have ",currentMoney," Inside your account.")
break
elif (atm == 2):
money = int(input("How Much money do you want to deposit? "))
depositedMoney+=money
currentMoney=depositedMoney
print("You Have ",currentMoney," Inside Your Account")
break
elif (atm == 3):
while True:
takeMoney = int(input("How much money do you want to take? "))
if (takeMoney > currentMoney):
print("You don't have that value.")
continue
else:
print("LOADING...")
time.sleep(3)
takenMoney+=takeMoney
print("You've taken ",takenMoney," , You now have "(currentMoney-takenMoney," In your account")
break
Main()
每当我尝试运行它时,它会突出显示上面的" break" ,当我删除它时,会弹出另一个错误,即" Main()"在最后一个代码,"和"它一直这样做......
我希望我能找到答案。
答案 0 :(得分:0)
我的代码正常运行。根据你对编程不熟悉的事物的看法。您的主要问题是,如果您不将它们作为参数包含在内,则无法从函数内部访问变量。我建议你看一下this tutorial。
工作代码:
import time
#Initializing Variables
currentMoney = 0
depositedMoney = 0
takenMoney = 0
#Main Fucntion which shows when the program starts
def Main(currentMoney, depositedMoney, takenMoney): # arguments were missing
while True:
try:
userChoice = int(input("Welcome to the ATM Organizer. To Preceed Enter 1 To Close Enter 0")) # "int()" was missing. had to add try and except as well
except ValueError:
print("You didn't choose what was given!")
continue
if userChoice == 1:
currentMoney, depositedMoney, takenMoney = Atm(currentMoney, depositedMoney, takenMoney) # was missing
elif userChoice == 0:
print("Thank you.Good Bye!")
break
def Atm(currentMoney, depositedMoney, takenMoney): # arguments were missing
#Handling Invalid Inputs # comment sign was missing
while True:
try:
atm = int(input("Welcome Inside The ATM , To See your money , Type '1' , To put money to the cash machine , Type '2' , To take money out of the ATM , Type '3' , To Exit the ATM , Type '0' ."))
except ValueError:
print("You didn't choose what was given!")
continue
#Input Choices # comment sign was missing
if (atm == 0):
return currentMoney, depositedMoney, takenMoney # was missing
elif (atm == 1):
print("You Have ",currentMoney," Inside your account.")
return currentMoney, depositedMoney, takenMoney # was missing
elif (atm == 2):
money = int(input("How Much money do you want to deposit? "))
depositedMoney+=money
currentMoney=depositedMoney
print("You Have ",currentMoney," Inside Your Account")
return currentMoney, depositedMoney, takenMoney # was missing
elif (atm == 3):
while True:
takeMoney = int(input("How much money do you want to take? "))
if (takeMoney > currentMoney):
print("You don't have that value.")
continue
else:
print("LOADING...")
time.sleep(3)
takenMoney+=takeMoney
print("You've taken ",takenMoney," , You now have ",(currentMoney-takenMoney)," In your account") # ")" was missing, "," was missing
return currentMoney, depositedMoney, takenMoney # was missing
Main(currentMoney, depositedMoney, takenMoney) # arguments were missing