Python Don';我不知道如何使用创建的方法
我对Python和一般编程非常陌生。我正在跟随youtube上的一个例子,讲述如何制作一个简单的计算器,但我想添加我的想法和功能。或多或少,我想让它变得多才多艺 这是一个不适合我的部分。我不知道如何使用我创建的两种方法使其工作Python Don';我不知道如何使用创建的方法,python,Python,我对Python和一般编程非常陌生。我正在跟随youtube上的一个例子,讲述如何制作一个简单的计算器,但我想添加我的想法和功能。或多或少,我想让它变得多才多艺 这是一个不适合我的部分。我不知道如何使用我创建的两种方法使其工作 def main(): while True: Num1() num1 = int.num1() Num2() num2 = int.num2() # Executing the specified calculations wit
def main():
while True:
Num1()
num1 = int.num1()
Num2()
num2 = int.num2()
# Executing the specified calculations with the 'Operation' function (row 13)
Operation(num1, num2)
# --> followed by another nested while asking if the user wants to make another calculation
我希望应用程序在任何地方都能读到,如果用户键入“exit”,它将退出,即使它需要一个数字,所以我创建了两个方法来这样做,因为它无法直接从main()处理循环和用户输入
问题是,我得到了这个错误,我得到了它,但我只是不知道如何去做
Traceback (most recent call last):
File "C:/Users/Albert/PycharmProjects/HelloWorld - First Py'
Apps/SimpleCalculator.py", line 131, in <module>
main()
File "C:/Users/Albert/PycharmProjects/HelloWorld - First Py'
Apps/SimpleCalculator.py", line 109, in main
Operation(num1, num2)
NameError: name 'num1' is not defined
回溯(最近一次呼叫最后一次):
文件“C:/Users/Albert/PycharmProjects/HelloWorld-First Py”
Apps/SimpleCalculator.py”,第131行,在
main()
文件“C:/Users/Albert/PycharmProjects/HelloWorld-First Py”
Apps/SimpleCalculator.py”,第109行,主视图
操作(num1,num2)
NameError:未定义名称“num1”
区分大小写,您定义的Num1()&Num2()
不是Num1()&Num2()
您的代码有很多错误
def ask_number():
while True:
num = input("What is the first number? ")
if num.isdigit():
return int(num) #This line fixes problem 2
elif num == 'exit':
print("Thank you using the SimpleCalculator. Bye!")
time.sleep(3)
exit()
else:
print("I don't understand. Please input a valid number")
def getnum():
while True:
mynum = input("What is the first number? ")
if mynum.isdigit():
break
elif mynum == 'exit':
print("Thank you using the SimpleCalculator. Bye!")
time.sleep(3)
exit()
else:
print("I don't understand. Please input a valid number")
return mynum
def main():
while True:
num1 = int(getnum())
num2 = int(getnum())
Num1()
num1 = int.num1()
Num2()
num2 = int.num2()
与
我们两次调用ask_number函数,并将其返回值赋给num1和num2。问题是num1()和num2()提出了问题,但没有对它们做任何处理。此外,所定义的num1和num2是函数的局部变量,因此无法从main()函数访问它们。您应该添加“return numX”而不是break,并将Num1()赋值给Num1,这样可以在main()函数可访问的变量中捕获用户的输入
def ask_number():
while True:
num = input("What is the first number? ")
if num.isdigit():
return int(num) #This line fixes problem 2
elif num == 'exit':
print("Thank you using the SimpleCalculator. Bye!")
time.sleep(3)
exit()
else:
print("I don't understand. Please input a valid number")
def getnum():
while True:
mynum = input("What is the first number? ")
if mynum.isdigit():
break
elif mynum == 'exit':
print("Thank you using the SimpleCalculator. Bye!")
time.sleep(3)
exit()
else:
print("I don't understand. Please input a valid number")
return mynum
def main():
while True:
num1 = int(getnum())
num2 = int(getnum())
您的代码应该是这样的:
import time
def Num1():
while True:
num1=input("What is the first number? ")
if num1.isdigit():
return int(num1)
elif num1 == 'exit':
print("Thank you using the SimpleCalculator. Bye!")
time.sleep(3)
exit()
else:
print("I don't understand. Please input a valid number")
def Num2():
while True:
num2=input("What is the first number? ")
if num2.isdigit():
return int(num2)
elif num2 == 'exit':
print("Thank you using the SimpleCalculator. Bye!")
time.sleep(3)
exit()
else:
print("I don't understand. Please input a valid number")
def Operation(num_1, num_2):
# code for Operation function
pass
def main():
while True:
num1=Num1()
num2=Num2()
Operation(num1, num2)
if __name__=="__main__": main()
免责声明:我想要求一些澄清,但我还没有SO特权 运行程序时,能否确保缩进正确?例如,while循环不应与函数定义处于同一缩进级别 另外,您的函数都在这个文件
SimpleCalculator.py
中吗?如果是这样的话,您将需要有一个额外的行来调用您的方法,因为现在它们只被声明
if __name__ == '__main__':
# And the following line will call your main function which you defined previously
main()
更好的是,只需删除main()
函数,并用更具python风格的语法替换它:
if __name__ == '__main__':
while True:
Num1()
Num2()
另外,由于您正在调用Num1()
和Num2()
并且它们正在处理输入,因此您不需要设置Num1=int.Num1()
或num=int.Num2()
(这两个都会给我错误)或类似的设置。如果希望将值返回到main方法进行处理,则需要在选中If num1.isdigit():
时执行return num1
,并在main方法集中执行firstNumberInput=num1()
希望这有帮助 可能只是您发布了代码,但有几点需要解决 函数Num1和Num2不返回任何内容。尽管num1值是在函数内分配的,但在该函数的作用域之外是不可访问的 int没有方法“num1”,因此调用int.num1()应替换为int(num1()) 创建两个几乎相同的函数会为您自己创建更多的工作,并且代码很难更改,您可以创建一个更通用的函数
def ask_number():
while True:
num = input("What is the first number? ")
if num.isdigit():
return int(num) #This line fixes problem 2
elif num == 'exit':
print("Thank you using the SimpleCalculator. Bye!")
time.sleep(3)
exit()
else:
print("I don't understand. Please input a valid number")
def getnum():
while True:
mynum = input("What is the first number? ")
if mynum.isdigit():
break
elif mynum == 'exit':
print("Thank you using the SimpleCalculator. Bye!")
time.sleep(3)
exit()
else:
print("I don't understand. Please input a valid number")
return mynum
def main():
while True:
num1 = int(getnum())
num2 = int(getnum())
我在您的代码中看到一些常见错误:
def main():
while True:
# just call NumEntering - one general function for entering numbers,
# and give to it attribute - message for user.
num1 = int(NumEntering("What is the first number? "))
num2 = int(NumEntering("What is the second number? "))
# Executing the specified calculations with the 'Operation' function (row 13)
Operation(num1, num2)
以及功能:
def NumEntering(message): # message - this what function get from outer function.
while True:
num_str = input(message) # show given message
if num_str.isdigit():
return int(num_str) # convert to int and return to outer function
elif num_str == 'exit':
print("Thank you using the SimpleCalculator. Bye!")
time.sleep(3)
exit()
else:
print("I don't understand. Please input a valid number")
continue
嗨,谢谢你的意见。我听从了你的建议。工作起来很有魅力。就像我想要的一样。干杯嗨,非常感谢。谢谢,所有函数都在同一个文件中。然而,我现在正在从另一个程序中使用它,是的,我包括了从外部调用方法的行,谢谢!