首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如果没有必要,Python代码重复8次。

如果没有必要,Python代码重复8次。
EN

Stack Overflow用户
提问于 2017-09-17 13:55:51
回答 1查看 39关注 0票数 0
代码语言:javascript
复制
def Reset():
    global seven_digit
    seven_digit = ["","","","","","",""]
    global x
    x = 0
    global eight
    eight = 0
    global c
    c = 0
    cinput()
def cinput():
    global thing
    print("Enter digit ", x+1)
    thing = input("")
    check()
def check():
    global eight
    global x
    if not thing.isdigit():
        print("That character is not allowed")
        cinput()
    elif len(thing) > 1:
        print("Those characters are not allowed")
        cinput()
    if x < 7:
        seven_digit[x] = int(thing)
        x += 1
        cinput()
   if x == 7:
        eight = int(thing)
        fcheck()
def fcheck(): #this section is temporary just for testing
    global c 
    c+=1
    print("This is c, ", c)
    print("Test")
    print(seven_digit)
    print(eight)
Reset()

这是我一直作为a级任务开发的代码(这是今年的GCSE课程),但是我遇到了一个问题,在自创建函数fcheck()中的最后一部分重复了8次。我以前在python中使用过类似的过程,以前从未见过这样的错误。我想知道有没有人知道我能做些什么来解决这个问题,谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-09-17 15:01:54

checkcinput之间有一个相互调用,所以在这个调用链中调用fcheck,它将被调用8次。

如果您想要调用fcheck一次,在所有的计算链之后,您可以在check的最后一行删除对它的调用,然后在Reset的末尾调用它。

代码语言:javascript
复制
def Reset():
    global seven_digit
    seven_digit = ["","","","","","",""]
    global x
    x = 0
    global eight
    eight = 0
    global c
    c = 0
    cinput()
    fcheck()

def cinput():
    global thing
    print("Enter digit ", x+1)
    thing = str(input(""))
    check()

def check():
    global eight
    global x
    if not thing.isdigit():
        print("That character is not allowed")
        cinput()
    elif len(thing) > 1:
        print("Those characters are not allowed")
        cinput()
    if x < 7:
        seven_digit[x] = int(thing)
        x += 1
        cinput()
    if x == 7:
        eight = int(thing)

def fcheck(): #this section is temporary just for testing
    global c
    c+=1
    print("This is c, ", c)
    print("Test")
    print(seven_digit)
    print(eight)

Reset()
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46264840

复制
相关文章

相似问题

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