甚至在程序退出后也会调用Python函数

时间:2019-09-28 23:38:44

标签: python

我正在尝试使用python创建一个简单的以10为基础的X转换器(只是开始学习它),并且我已经或多或少地处于一种工作状态。唯一的问题是我的重新启动功能,该功能会询问用户是否要转换另一个数字。如果用户同意转换另一个数字(调用数字输入函数等),但即使他们选择不转换,该函数也会运行。

除了通常广泛的谷歌搜索外,我已经尝试将重新启动功能放在转换器功能内,将故障和重新启动功能都放在转换器内,更改功能在代码内的位置,仔细检查所有代码,以确保没有在不应调用的位置调用输入函数。我怀疑转换器函数终止于调用输入函数(创建循环[?])的地方可能是一个问题,但是即使我这样做是正确的,我也不知道如何修复它。

这是重新启动功能。除其他输入提示不同外,“ fail()”功能完全相同。

import sys

dec = 1 # global variable declaration
conBase = 1 # global variable declaration

def main()
  def fail():
    resyn = input('\nPlease type either Y or N (lowercase also works!)')
    if resyn == 'y' or resyn == 'Y':
      errordetect()
    elif resyn == 'n' or resyn == 'N':
      print':('
      sys.exit()  
    else:
      fail()

  def restart():
    resyn = input('\n\nConvert another number [Y/N]?')
    if resyn == 'y' or resyn == 'Y':
      errordetect()
    elif resyn == 'n' or resyn == 'N':
      print':('
      sys.exit()
    else:
      fail()

  def converter(): # converter function - hub for all conversion algorithms
    global dec # global variable declaration
    global conBase # global variable declaration
    print'\n','since', dec, 'is is going to be converted to base', conBase,',it will read bottom to top rather than left to right'
    print'Decimal number', dec, 'converted to base', conBase, 'is equal to\n'

    while int(dec) > 0: # base converter
      decrem = int(dec) % int(conBase) # establishes decimal remainder as avalue
      if decrem == 10: # prints a replacement value if the remainder is 10
        print'A'
      elif decrem == 11: # prints a replacement value if the remainder is 11
        print'B'
      elif decrem == 12: # prints a replacement value if the remainder is 12
        print'C'
      elif decrem == 13: # prints a replacement value if the remainder is 13
        print'D'
      elif decrem == 14: # prints a replacement value if the remainder is 14
        print'E'
      elif decrem == 15: # prints a replacement value if the remainder is 15
        print'F'
      elif decrem == 16: # prints a replacement value if the remainder is 16
        print'G'
      elif decrem == 17: # prints a replacement value if the remainder is 17
        print'H'
      elif decrem == 18: # prints a replacement value if the remainder is 18
        print'I'
      elif decrem == 19: # prints a replacement value if the remainder is 19
        print'J'
      elif decrem == 20: # prints a replacement value if the remainder is 20
        print'K'
      elif decrem == 21: # prints a replacement value if the remainder is 21
        print'L'
      elif decrem == 22: # prints a replacement value if the remainder is 22
        print'M'
      elif decrem == 23: # prints a replacement value if the remainder is 23
        print'N'
      elif decrem == 24: # prints a replacement value if the remainder is 24
        print'O'
      elif decrem == 25: # prints a replacement value if the remainder is 25
        print'P'
      elif decrem == 26: # prints a replacement value if the remainder is 26
        print'Q'
      elif decrem == 27: # prints a replacement value if the remainder is 27
        print'R'
      elif decrem == 28: # prints a replacement value if the remainder is 28
        print'S'
      elif decrem == 29: # prints a replacement value if the remainder is 29
        print'T'
      elif decrem == 30: # prints a replacement value if the remainder is 30
        print'U'
      elif decrem == 31: # prints a replacement value if the remainder is 31
        print'V'
      elif decrem == 32: # prints a replacement value if the remainder is 32
        print'W'
      elif decrem == 33: # prints a replacement value if the remainder is 33
        print'X'
      elif decrem == 34: # prints a replacement value if the remainder is 34
        print'Y'
      elif decrem == 35: # prints a replacement value if the remainder is 35
        print'Z'
      else:
        print(decrem) # prints the remainder if no replacement is needed
      dec = int(dec) / int(conBase) # divides current value of decimal numberby base
    restart()

  def errordetect():
    global dec
    global conBase
    dec = input('\nInput your decimal number here:') # define decimal variable
    decLen = len(dec)
    decChar = 0
    decLen = int(decLen)
    for decLen in dec:
      if dec[decChar] < '0' or dec[decChar] > '9':
        forceResponse = input('\nThere is a non-integer in your input! Typeanything to try again:')
        if forceResponse != 'dontdoit':
          errordetect()
      decChar = decChar + 1

    conBase = input('\nConvert decimal {} to base [2-36]:'.format(dec))
    try:
      if int(conBase) <= 36 and int(conBase) >= 2:
        converter()
      else:
        forceResponse = input('\nThat base is invalid! Type anything to tryagain:')
        if forceResponse != 'dontdoit':
          errordetect()
    except:
      forceResponse = input('\nThere is a non-integer in your input! Type anything to try again:')
      if forceResponse != 'dontdoit':
        errordetect()
    else:
      converter()

    errordetect()

main()

我应该认为这将导致重新启动函数在除法过程结束时被调用,而没有其他任何事情,但是如果用户键入“ n”或“ N”,则当输入失败时我会得到输出12月的字母检查。

我尝试不包含任何不必要的代码,但是我确实意识到这可能要花很长时间。如果是这种情况,请事先道歉,但是此问题需要很多背景信息。

编辑:粘贴整个代码

0 个答案:

没有答案