Python Forum
Thread Rating:
  • 2 Vote(s) - 1.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Random Shuffle List
#1
Hello, im trying to make a kind of test and i want the questions to shuffle with random.shuffle.. Anyone understand what im saying?

h
emlig = 1

print("Question 1")
gissning = int( input() )

while( hemlig != gissning ):
    print("FALSE")
    gissning = int( input() )

print("CORRECT")

hemlig = 2

print("Question 2")
gissning = int( input() )

while( hemlig != gissning ):
    print("FALSE")
    gissning = int( input() )

print("CORRECT")

hemlig = 3

print("Question 3")
gissning = int( input() )

while( hemlig != gissning ):
    print("FALSE")
    gissning = int( input() )

print("CORRECT")
Reply
#2
I don't understand, the code has no list of questions to be shuffled and does not import random or use random.shuffle.
Reply
#3
okey im sorry, the code ATM, is making questions, and if i write incorrect answer it prints out FALSE, if i write correct answer, it prints out Correct,

After i answer Question 1, and get correct answer, it prints out Question 2, but i want it to be random, so it maybe it starts with Question 2, and after it prints out Question 1 or 3, just so it is random..

Dont know how to explain, this is my third time with python
Reply
#4
use the random module
>>> import random
>>> lister = [1,2,3]
>>> random.choice(lister)
3
>>> random.choice(lister)
1
>>> random.choice(lister)
3
>>> random.choice(lister)
3
>>> random.choice(lister)
2
and one method is then you can make your question and correct answers as a dict such as
questions = {
    'answer 1':'Question 1',
    'answer 1':'Question 2',
    'answer 1':'Question 3',
}
Recommended Tutorials:
Reply
#5
Do you know how to use lists, loops and functions?
You  could make a list that has a (question,  answer) tuple, shuffle the list, loop the list passing each iteration to a function that deals with asking for input agianst the passed in question and checks the input against the passed in answer.
Reply
#6
(Oct-20-2016, 12:11 PM)Yoriz Wrote: Do you know how to use lists, loops and functions?
You  could make a list that has a (question,  answer) tuple, shuffle the list, loop the list passing each iteration to a function that deals with asking for input agianst the passed in question and checks the input against the passed in answer.

hmm, i barely know how to use lists, loops and functions really...

(Oct-20-2016, 12:08 PM)metulburr Wrote: use the random module
>>> import random >>> lister = [1,2,3] >>> random.choice(lister) 3 >>> random.choice(lister) 1 >>> random.choice(lister) 3 >>> random.choice(lister) 3 >>> random.choice(lister) 2 
and one method is then you can make your question and correct answers as a dict such as
questions = {     'answer 1':'Question 1',     'answer 1':'Question 2',     'answer 1':'Question 3', }
the second type looks easier but i need to use random.shuffle for this?
Reply
#7
(Oct-20-2016, 12:35 PM)simon Wrote: hmm, i barely know how to use lists, loops and functions really...
Then you would need to learn that first
http://python-forum.io/Thread-List-basic
http://python-forum.io/Thread-Dictionaries-basic
http://python-forum.io/Thread-Function-B...ight=basic

In fact i would skim though all the tutorials with the word basic in it.
Recommended Tutorials:
Reply
#8
(Oct-20-2016, 12:38 PM)metulburr Wrote:
(Oct-20-2016, 12:35 PM)simon Wrote: hmm, i barely know how to use lists, loops and functions really...
Then you would need to learn that first http://python-forum.io/Thread-List-basic http://python-forum.io/Thread-Dictionaries-basic http://python-forum.io/Thread-Function-B...ight=basic
okey thank you!
Reply
#9
I have now updated my script but i dont know what im doing wrong.. i think it is something wrong with the if or input.. if anyone wants to try it out and give me a little feedback i would appreciate that :P

my goal is to print out Random Question, and when i answer correct on that it prints out Correct!, if im wrong it should print out False!

import random 

#variables for questions
fr1=("Fråga 1?")
fr2=("Fråga 2?")
fr3=("Fråga 3?")
fr4=("Fråga 4?")
fr5=("Fråga 5?")
fr6=("Fråga 6?")
fr7=("Fråga 7?")
fr8=("Fråga 8?")
fr9=("Fråga 9?")
fr10=("Fråga 10?")

#variables for answers
sv1=("Svar 1")
sv2=("Svar 2")
sv3=("Svar 3")
sv4=("Svar 4")
sv5=("Svar 5")
sv6=("Svar 6")
sv7=("Svar 7")
sv8=("Svar 8")
sv9=("Svar 9")
sv10=("Svar 10")

#variables for questions
fr = [fr1, fr2, fr3, fr4, fr5, fr6, fr7, fr8, fr9, fr10]
sv = [sv1, sv2, sv3, sv4, sv5, sv6, sv7, sv8, sv9, sv10]

#KEYS
fr1=sv1
fr2=sv2
fr3=sv3
fr4=sv4
fr5=sv5
fr6=sv6
fr7=sv7
fr8=sv8
fr9=sv9
fr10=sv10

#variables for random.choice that prints out randomquestions from the "fr" list
slumpFråga = random.choice(fr)
svar = 0

print(slumpFråga)   #random question
input()
int()
   
if sv==slumpFråga:     #if-sats answer = randomquestion
       print ("Correct!")

else:   
       print ("False!")
   
Reply
#10
Well, you have a lot going on and to be honest, makes very little sense. For instance, in line 45 you have "svar = 0" yet you never use "svar".  Line 49 you have "int()"  but it relates to nothing. Line 51, you have 

if sv == slumpFråga:  # if-sats answer = randomquestion

    print("Correct!")
but "sv" will never be comparable to "slumpFraga" because "sv" is a list and "slumpFraga" is a string, so you will always get "False"

You really need to understand the basics. The link posted by metulbuur is very good and there are many, many tutorials out there for beginners.

Here is a very simply example, see if you can follow along with what is happening.

import random

question = ["Are you happy", "Are you sad"]
choices = ["yes", "no"]
rand_question = random.choice(question)
print("The question is: ", rand_question)
answer = input("What is your answer? ")

if answer in choices:
    print("Your answer is True")
else:
    print("Your answer is False")
Start off small and once it works, slowly build up from there.  Use variable names that make sense. When you print something, make sure you let yourself and others know what it is your printing. And use "print" statements liberally, to make sure variables contain what they ought to contain and your program is progressing as you intend.

Good luck  Smile
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Random selection from list Mohsky 1 10,238 Mar-31-2020, 10:27 PM
Last Post: deanhystad
  printing list of random generated rectangles Zatoichi 8 7,351 Feb-18-2018, 06:34 PM
Last Post: buran
  bubble sort random list atux_null 7 7,908 Nov-03-2017, 07:28 PM
Last Post: sparkz_alot
  List with Random Numbers AnjyilLee 5 9,367 Oct-14-2017, 09:22 PM
Last Post: buran
  Using two functions to shuffle a deck of cards bwe587 7 9,208 Feb-15-2017, 08:15 AM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020