سلام من در حال یادگیری پایتون هستم
یه کد توی vs code نوشتم و ماشین حساب درست کردم و الان دارم ارور هاش رو تنظیم میکنم و فکر میکنم آخرای کارم
اینم کد:
import os
def calc(a):
num1 = ""
num2 = ""
turn = 1
operation = ""
for select in str(a):
if(select in "0123456789 "):
if(turn == 1):
num1 += select
else:
num2 += select
elif(select in "+-*/%<>"):
if(num1 == ""):
print("invalid")
global Error
Error = True
break
else:
turn = 2
operation += select
else:
print("invalid")
break
if(Error):
Error = False
elif(num2 == ""):
print("invalid")
else:
if(operation == "+"):
print(int(num1) + int(num2))
elif(operation == "-"):
print(int(num1) - int(num2))
elif(operation == "*"):
print(int(num1) * int(num2))
elif(operation == "/"):
print(int(num1) / int(num2))
elif(operation == "**"):
print(int(num1) ** int(num2))
elif(operation == "%"):
print(int(num1) % int(num2))
elif(operation == "<"):
if(int(num1) < int(num2)):
print("Yes")
else:
print("No")
elif(operation == ">"):
if(int(num1) > int(num2)):
print("Yes")
else:
print("No")
else:
print("invalid")
os.system("cls")
print("\nWelcome To Calculator!\nEnter 'Z' To Exit\nEnter 'C' To Clear")
print(20 * "--" + "\n")
while True:
Entrance = input()
if(Entrance in "zZ"):
print("Okay")
break
if(Entrance in "cC"):
os.system("cls")
print("\nWelcome To Calculator!\nEnter 'Z' To Exit\nEnter 'C' To Clear")
print(20 * "--" + "\n")
continue
calc(Entrance)
فقط یه مشکل دارم اونم اینه که من اینجا متغیر Error رو تنظیم کردم ولی....
def calc(a):
num1 = ""
num2 = ""
turn = 1
operation = ""
for select in str(a):
if(select in "0123456789 "):
if(turn == 1):
num1 += select
else:
num2 += select
elif(select in "+-*/%<>"):
if(num1 == ""):
print("invalid")
global Error
Error = True
break
else:
turn = 2
operation += select
else:
print("invalid")
break
وقتی میخوام توی if ازش استفاده کنم میزنه تعریف نشده
اینجا گذاشتم ببینید مشکلی داره؟
if(Error):
Error = False
اینم ارورش
File "c:\Users\CaratechCO\Desktop\code\py2\Calculator.py", line 71, in <module>
calc(Entrance)
File "c:\Users\CaratechCO\Desktop\code\py2\Calculator.py", line 26, in calc
if(Error):
^^^^^
NameError: name 'Error' is not defined. Did you mean: 'OSError'?
PS C:\Users\CaratechCO\Desktop\code\py2>
اصلا خطای عجیبی نیست شما فقط متغیر(variable) Error رو تعریف نکرده
و اشتباها در خود شرطت واردش کرده بودی سعی کن نام های متغیرت رو هرگز شبیه به متد های خود زبان
پایتون ننویسی
import os
def calc(a):
thiserror = False # همون متغیر ارور که خارج از شرط هات باید میزاشتی
num1 = ""
num2 = ""
turn = 1
operation = ""
for select in str(a):
if (select in "0123456789 "):
if (turn == 1):
num1 += select
else:
num2 += select
elif (select in "+-*/%<>"):
if (num1 == ""):
print("invalid")
thiserror = True
break
else:
turn = 2
operation += select
else:
print("invalid")
break
if (thiserror):
thiserror = False
elif (num2 == ""):
print("invalid")
else:
if (operation == "+"):
print(int(num1) + int(num2))
elif (operation == "-"):
print(int(num1) - int(num2))
elif (operation == "*"):
print(int(num1) * int(num2))
elif (operation == "/"):
print(int(num1) / int(num2))
elif (operation == "**"):
print(int(num1) ** int(num2))
elif (operation == "%"):
print(int(num1) % int(num2))
elif (operation == "<"):
if (int(num1) < int(num2)):
print("Yes")
else:
print("No")
elif (operation == ">"):
if (int(num1) > int(num2)):
print("Yes")
else:
print("No")
else:
print("invalid")
os.system("cls")
print("\nWelcome To Calculator!\nEnter 'Z' To Exit\nEnter 'C' To Clear")
print(20 * "--" + "\n")
while True:
Entrance = input()
if (Entrance in "zZ"):
print("Okay")
break
if (Entrance in "cC"):
os.system("cls")
print("\nWelcome To Calculator!\nEnter 'Z' To Exit\nEnter 'C' To Clear")
print(20 * "--" + "\n")
continue
calc(Entrance)
آیا مایل به ارسال نوتیفیکیشن و اخبار از طرف راکت هستید ؟