首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >我还在学习python,我不明白为什么我会把它当成一个错误

我还在学习python,我不明白为什么我会把它当成一个错误
EN

Stack Overflow用户
提问于 2022-09-30 02:31:51
回答 1查看 39关注 0票数 0
代码语言:javascript
复制
print("Calculator!!!")
loop = 1
#title, obv
#basic inputs
numbercount = int(input("How many numbers are you doing math with? max is 3: "))


while loop == 1:
  if numbercount == 1:
    print("min characters are 2")
  elif numbercount == 2:
    mathtype = int(input("1. Addition\n2. Subtraction\n3. Multiplcation\n4. Division\n"))
  if mathtype == 1:
    print("format as\na = [number]\nb = [number]\n\n")
    a = int(input("a = "))
    b = int(input("b = "))
    c = a + b
    print("Your awnser is: ", c)
  elif mathtype == 2:
    print("format as\na = [number]\nb = [number]\n\n")
    a = int(input("a = "))
    b = int(input("b = "))
    c = a - b
    print("Your awnser is: ", c)
  elif mathtype == 3:
    print("format as\na = [number]\nb = [number]\n\n")
    a = int(input("a = "))
    b = int(input("b = "))
    c = a * b
    print("Your awnser is: ", c)
  elif mathtype == 4:
    print("format as\na = [number]\nb = [number]\n\n")
    a = int(input("a = "))
    b = int(input("b = "))
    c = a / b
    print("Your awnser is: ", c)
  else:
    print("try again")
  if numbercount == 3:
    mathtype = int(input("1. Addition\n2. Subtraction\n3. Multiplcation\n4. Division\n"))
  if mathtype == 1:
    print("format as\na = [number]\nb = [number]\nc = [number]\n\n")
    a = int(input("a = "))
    b = int(input("b = "))
    c = int(input("c = "))
    d = a + b + c
    print("Your awnser is: ", c)
  elif mathtype == 2:
    print("format as\na = [number]\nb = [number]\nc = [number]\n\n")
    a = int(input("a = "))
    b = int(input("b = "))
    c = int(input("c = "))
    d = a - b - c
    print("Your awnser is: ", c)
  elif mathtype == 3:
    print("format as\na = [number]\nb = [number]\nc = [number]\n\n")
    a = int(input("a = "))
    b = int(input("b = "))
    c = int(input("c = "))
    d = a * b * c
    print("Your awnser is: ", c)
  elif mathtype == 4:
    print("format as\na = [number]\nb = [number]\nc = [number]\n\n")
    a = int(input("a = "))
    b = int(input("b = "))
    c = int(input("c = "))
    d = a / b / c
    print("Your awnser is: ", c)
  else:
    print("try again")
else:
  print("Max Characters is 3")

错误信息:计算器!你在用多少个数字做数学?max为3: 3回溯(最近一次调用):文件"main.py",第13行,如果mathtype == 1: NameError: name 'mathtype‘未定义

我对python还是比较陌生的,我需要一些帮助:/除了为什么要使用python之外,还有什么会有帮助的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-09-30 02:38:25

您在"elif“语句中定义了mathtype。这意味着如果if语句之前为true,那么elif语句中的代码将不会运行,这意味着整数类型将永远不会被定义。要解决这个问题,只需将第一个输入放入另一个while循环中,因此它将继续请求编号:

代码语言:javascript
复制
print("Calculator!!!")
loop = 1
#title, obv
#basic inputs


while loop == 1:
  while True:
    numbercount = int(input("How many numbers are you doing math with? max is 3: "))
    if numbercount == 1:
      print("min characters are 2")
    else:
      break
  mathtype = int(input("1. Addition\n2. Subtraction\n3. Multiplcation\n4. Division\n"))
  if mathtype == 1:
    print("format as\na = [number]\nb = [number]\n\n")
    a = int(input("a = "))
    b = int(input("b = "))
    c = a + b
    print("Your awnser is: ", c)
  elif mathtype == 2:
    print("format as\na = [number]\nb = [number]\n\n")
    a = int(input("a = "))
    b = int(input("b = "))
    c = a - b
    print("Your awnser is: ", c)
  elif mathtype == 3:
    print("format as\na = [number]\nb = [number]\n\n")
    a = int(input("a = "))
    b = int(input("b = "))
    c = a * b
    print("Your awnser is: ", c)
  elif mathtype == 4:
    print("format as\na = [number]\nb = [number]\n\n")
    a = int(input("a = "))
    b = int(input("b = "))
    c = a / b
    print("Your awnser is: ", c)
  else:
    print("try again")
  if numbercount == 3:
    mathtype = int(input("1. Addition\n2. Subtraction\n3. Multiplcation\n4. Division\n"))
  if mathtype == 1:
    print("format as\na = [number]\nb = [number]\nc = [number]\n\n")
    a = int(input("a = "))
    b = int(input("b = "))
    c = int(input("c = "))
    d = a + b + c
    print("Your awnser is: ", c)
  elif mathtype == 2:
    print("format as\na = [number]\nb = [number]\nc = [number]\n\n")
    a = int(input("a = "))
    b = int(input("b = "))
    c = int(input("c = "))
    d = a - b - c
    print("Your awnser is: ", c)
  elif mathtype == 3:
    print("format as\na = [number]\nb = [number]\nc = [number]\n\n")
    a = int(input("a = "))
    b = int(input("b = "))
    c = int(input("c = "))
    d = a * b * c
    print("Your awnser is: ", c)
  elif mathtype == 4:
    print("format as\na = [number]\nb = [number]\nc = [number]\n\n")
    a = int(input("a = "))
    b = int(input("b = "))
    c = int(input("c = "))
    d = a / b / c
    print("Your awnser is: ", c)
  else:
    print("try again")
else:
  print("Max Characters is 3")
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73903429

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档