简体   繁体   English

如何再次经历while循环?

[英]How to go through while loop again?

Im making a simple question and answer app in python, in this specific example it displays a word in simple chinese and gives two answers to pick wrong or right, i'm struggling to present a new question without restarting my code.我在 python 中制作了一个简单的问答应用程序,在这个特定的示例中,它用简单的中文显示一个单词,并给出了两个答案来选择错误或正确的,我正在努力提出一个新问题而不重新启动我的代码。 This my go at making an app that helps me learn chinese and I wanted to use minimum help, hence the weird code.这是我制作一个帮助我学习中文的应用程序的目标,我想使用最少的帮助,因此出现了奇怪的代码。

For example: the question is what is 1+1 and the user answered two then I want to go thourgh the code again an present the next question.例如:问题是什么是 1+1,用户回答了两个,然后我想再次检查代码并提出下一个问题。

the specific section im trying the run from inside a function, so when the user answers correctly or incorrectly by pressing the button in the function I want to go through the code again and present my next question我正在尝试从函数内部运行的特定部分,因此当用户通过按下函数中的按钮正确或错误地回答时,我想再次检查代码并提出我的下一个问题

` `


    # ans
    def button_1(event):
        if Ans_option1.text == ans_or_not["ans"]:
            print('correct')
            return 'correct'

            

        else:
            print("incorrect")

    def button_2 (event):
        if Ans_option2.text == ans_or_not["ans"]:
            print('correct')
            return 'correct'

        else:
            print("incorrect")
    Ans_option1 = gp.Button(app,return_1(), button_1)
    Ans_option2 = gp.Button(app,return_2(),button_2)

    app.add(Ans_option1,3,2, align = 'center')
    app.add(Ans_option2,3,4, align = 'center')
    app.run()
    



` `

whole code整个代码

import gooeypie as gp 
import random

app = gp.GooeyPieApp ('hello')


app.set_size (1000,500)
i = 2
n =3
while True :
    # use dictionary instead of lists so we c an have an answer to the questions
    question_dict = {

            'xihuan' : 'like',
            'Wo': 'I',
            'Ni': 'you' 

        }
    # random picks a value from a list of values made here
    picker =  random.choice(list(question_dict.values()))
    # I remake the same list here again as the indexing stays the same then find the index for the random value
    ind = list(question_dict.values()).index(picker)

    # I make a list of the keys and they have mathing indexes to their values and we use the index number given to us previously to find the key
    final  = list(question_dict.keys())[ind]
    # print(final)
    test = 1
    def question ():
        

        question_dict.pop(final)
        print(question_dict)
        # return final
        
        return final
        

    ans_or_not = {

            # this works first before the item is popped so it can cause a they same two words to appear on the buttons
            # varialbe from inside the functions arent stected outside , making this whole dictionary meaningless
            'ans' : picker,
            'non' : random.choice(list(question_dict.values()))





        }
        

    print(ans_or_not["non"])
    print(ans_or_not["ans"])

    while ans_or_not["non"] == ans_or_not["ans"]:
        ans_or_not.pop('non')
        print(ans_or_not)
        ans_or_not['non'] = random.choice(list(question_dict.values()))
        

    print(ans_or_not["non"])
    print(ans_or_not["ans"])


    nums = random.randrange(1,3)
    print(nums)



    def return_1():


        # while anss == nons :
        #   anss = random.randrange(0,2)
        #   print(anss + ','+ nons)

        if nums == 1 :
            return ans_or_not["ans"]

        if nums == 2:
            return ans_or_not["non"]


    def return_2():


        # while anss == nons :
        #   anss = random.randrange(0,2)
        #   print(anss + ','+ nons)

        if nums == 1 :
            return ans_or_not["non"]

        elif nums == 2:
            return ans_or_not["ans"]







    # design and layout
    def menu_select (event):
        pass
        menu_path = ' > '.join(event.menu)
        status.text = menu_path


    app.add_menu_item('Menu 1', 'Item 1', menu_select)



    # grid setup
    app.set_grid(4,5)
        
    question_lbl = gp.Label(app,question())
    app.add(question_lbl,2,3, align = 'center')


    # ans
    def button_1(event):
        if Ans_option1.text == ans_or_not["ans"]:
            print('correct')
            return 'correct'

            

        else:
            print("incorrect")

    def button_2 (event):
        if Ans_option2.text == ans_or_not["ans"]:
            print('correct')
            return 'correct'

        else:
            print("incorrect")
    Ans_option1 = gp.Button(app,return_1(), button_1)
    Ans_option2 = gp.Button(app,return_2(),button_2)

    app.add(Ans_option1,3,2, align = 'center')
    app.add(Ans_option2,3,4, align = 'center')
    app.run()
    



    

What i've tried我试过的

i've tried using the continue function我试过使用继续功能

  • ways to restart while loops重新启动 while 循环的方法

You have the problem wrong.你把问题弄错了。

This is going to sound really complicated, but stay with me.这听起来真的很复杂,但请听我说。 So what app.run() does is start the program, and it displays what you have already added to it.所以 app.run() 所做的是启动程序,它会显示您已经添加的内容。 By putting app.run() in the while loop, despite calling it multiple times, will only call once.通过将 app.run() 放在 while 循环中,尽管多次调用它,但只会调用一次。 What you need to do is draw the label and buttons, run the app, then start the while loop.您需要做的是绘制标签和按钮,运行应用程序,然后启动 while 循环。 In fact, I wouldn't use a while loop here at all.事实上,我根本不会在这里使用 while 循环。 Instead, I would have is so you have a dictionary for the right and wrong answers, then simply modify that and the button text when you get the answer correct.相反,我会让你有一本关于正确和错误答案的字典,然后在你得到正确答案时简单地修改它和按钮文本。 I don't really understand your code, but it would look something like:我真的不明白你的代码,但它看起来像:

#function for choosing answer
#function for checking button one
#function for checking button two
#draw button one and two and label
app.run()

Also, you would have it so that if you got the answer right, you would choose a new one.此外,你会拥有它,这样如果你得到正确的答案,你就会选择一个新的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM