Home > Mobile >  How do I get the random question to not appear again?
How do I get the random question to not appear again?

Time:03-30

I can't seem to fix how to get my questions to not appear again after they have been shown once... Does anyone have any idea?

import random
from turtle import *
import time

def reset(x, y):
    print("Körs")
    clearscreen()

    # Här gör man det enklare för sig själv genom att kategorisera turtles.
    #Turtle()

    # Här väljer man vilken fart turtles skall ha när dom ritar
    speed(10)

    # här göms alla turtles
    hideturtle()

    # Här skriver man vad programmet heter och vilken färg bakgrunden har.
    title('Albins ofungerade spel')
    Screen().bgcolor("orange")

    quest = ('Courier', 15, 'italic')
    main = ('Courier', 25, 'italic')

    frågor = [1, 2, 3, 4, 5]

    computer_action = random.choice(frågor)

    # här ritas alla olika boxar och sidor
    l = 300
    w = 75

    penup()
    goto(-150, 100)
    pendown()

    forward(l)
    left(90)

    forward(w)
    left(90)

    forward(l)
    left(90)

    forward(w)
    left(90)

    penup()
    goto(-150, 0)
    pendown()

    forward(l)
    left(90)

    forward(w)
    left(90)

    forward(l)
    left(90)

    forward(w)
    left(90)

    penup()
    goto(-150, -100)
    pendown()

    forward(l)
    left(90)

    forward(w)
    left(90)

    forward(l)
    left(90)

    forward(w)
    left(90)

    penup()
    goto(-150, -200)
    pendown()

    forward(l)  # Forward turtle by l units
    left(90)  # Turn turtle by 90 degree

    forward(w)  # Forward turtle by w units
    left(90)  # Turn turtle by 90 degree

    forward(l)  # Forward turtle by l units
    left(90)  # Turn turtle by 90 degree

    forward(w)  # Forward turtle by w units
    left(90)  # Turn turtle by 90 degree

    def good():
        penup()
        setposition(0, -280)
        pendown()
        write("Rätt val! Tryck på skärmen för ny fråga", font=main, align='center')
        onscreenclick(reset)


    def bad():
        penup()
        setposition(0, -280)
        pendown()
        write("Fel val! Tryck på skärmen för ny fråga", font=main, align='center')
        onscreenclick(reset)



    if computer_action == 1:


        penup()
        setposition(0, 130)
        pendown()
        write("A. Danmark", font=quest, align='center')

        penup()
        setposition(0, 30)
        pendown()
        write("B. Sverige", font=quest, align='center')

        penup()
        setposition(0, -70)
        pendown()
        write("C. Finland", font=quest, align='center')

        penup()
        setposition(0, -170)
        pendown()
        write("D. Norge", font=quest, align='center')

        penup()
        setposition(0, 250)
        pendown()
        write("Vad heter det största landet i Norden?", font=main, align='center')

        onkeypress(good, 'b')
        onkeypress(bad, 'd')
        onkeypress(bad, 'c')
        onkeypress(bad, 'a')
        listen()



    elif computer_action == 2:

        penup()
        setposition(0, 130)
        pendown()
        write("A. Java", font=quest, align='center')

        penup()
        setposition(0, 30)
        pendown()
        write("B. C  ", font=quest, align='center')

        penup()
        setposition(0, -70)
        pendown()
        write("C. Python", font=quest, align='center')

        penup()
        setposition(0, -170)
        pendown()
        write("D. LUA", font=quest, align='center')

        penup()
        setposition(0, 250)
        pendown()
        write("Vad är William och Svens favorit språk?", font=main, align='center')

        onkeypress(good, 'd')
        onkeypress(bad, 'a')
        onkeypress(bad, 'c')
        onkeypress(bad, 'b')
        listen()

    elif computer_action == 3:

        penup()
        setposition(0, 130)
        pendown()
        write("A. Fredrik Reignfeldt", font=quest, align='center')

        penup()
        setposition(0, 30)
        pendown()
        write("B. Magdalenda Andersson", font=quest, align='center')

        penup()
        setposition(0, -70)
        pendown()
        write("C. Sven Blomqvist", font=quest, align='center')

        penup()
        setposition(0, -170)
        pendown()
        write("D. Martin Nilsson", font=quest, align='center')

        penup()
        setposition(0, 250)
        pendown()
        write("Vad heter Sveriges statsminister?", font=main, align='center')

        onkeypress(good, 'b')
        onkeypress(bad, 'a')
        onkeypress(bad, 'c')
        onkeypress(bad, 'd')
        listen()

    elif computer_action == 4:

        penup()
        setposition(0, 130)
        pendown()
        write("A. Joe Biden", font=quest, align='center')

        penup()
        setposition(0, 30)
        pendown()
        write("B. Bill Clinton", font=quest, align='center')

        penup()
        setposition(0, -70)
        pendown()
        write("C. Donald Trump", font=quest, align='center')

        penup()
        setposition(0, -170)
        pendown()
        write("D. Albin Winqvist", font=quest, align='center')

        penup()
        setposition(0, 250)
        pendown()
        write("Vad heter USA's president?", font=main, align='center')

        onkeypress(good, 'a')
        onkeypress(bad, 'd')
        onkeypress(bad, 'c')
        onkeypress(bad, 'b')
        listen()

    elif computer_action == 5:

        penup()
        setposition(0, 130)
        pendown()
        write("A. Borgarskolan", font=quest, align='center')

        penup()
        setposition(0, 30)
        pendown()
        write("B. Procivitas", font=quest, align='center')

        penup()
        setposition(0, -70)
        pendown()
        write("C. Bernadottegymnasiet", font=quest, align='center')

        penup()
        setposition(0, -170)
        pendown()
        write("D. Pauliskolan", font=quest, align='center')

        penup()
        setposition(0, 250)
        pendown()
        write("Vad är Malmös bästa skola?", font=main, align='center')

        onkeypress(good, 'c')
        onkeypress(bad, 'a')
        onkeypress(bad, 'd')
        onkeypress(bad, 'b')
        listen()








    onscreenclick(reset)


onscreenclick(reset)
done()

Sorry if my code seems sloppy, or anything similar, but I just need help with how to make a question not appear again after it appeared once. And I know turtle might not be the best, but it's the only thing I could think of when doing this simple project. Thanks for any help I get, it means alot!

CodePudding user response:

Pretty sure you have to say that computer_action=0 if you want to end or change the 0 with another number like 3 to continue with your questions

if computer_action == 1:

penup()
setposition(0, 130)
pendown()
write("A. Danmark", font=quest, align='center')

penup()
setposition(0, 30)
pendown()
write("B. Sverige", font=quest, align='center')

penup()
setposition(0, -70)
pendown()
write("C. Finland", font=quest, align='center')

penup()
setposition(0, -170)
pendown()
write("D. Norge", font=quest, align='center')

penup()
setposition(0, 250)
pendown()
write("Vad heter det största landet i Norden?", font=main, align='center')

onkeypress(good, 'b')
onkeypress(bad, 'd')
onkeypress(bad, 'c')
onkeypress(bad, 'a')
listen()
computer_action=0

CodePudding user response:

Take this line of code:

frågor = [1, 2, 3, 4, 5]

...and put it outside of the reset() function - i.e., into global space

Then...

computer_action = random.choice(frågor)
frågor.remove(computer_action)

In other words, you make a random selection from the list then you remove whatever you selected.

The problem is that you'll only be able to do this len(frågor) times. You'll need to figure out how to cope with that eventuality

CodePudding user response:

Define your intial frågor outside of your reset-function. Then modify your computer-action like the following:

frågor = [1, 2, 3, 4, 5]
def reset(x, y):
    #.... 
    random.shuffle(frågor)
    computer_action = frågor.pop()
     

pop returns the last element of the list frågor and deletes it from the list. So it can't ever be repeated.

Minor hint: avoid special characters like å in variables. Might result in problems when the encoding of your file changes.

  • Related