Python
Solved9 answers
-
jee pee ModeratorWell!!!
As I pointed out earlier, your code has 180 lines; we should be able to do the same thing in 25 lines ;-)
You repeat the same sequence 14 times for each question; the essence of a programmer's job is not to do the work but to make the computer do it. These 14 sequences could form a single repeated loop. As yg_be pointed out, you need to work on using loops with simple examples, codes with 5/6 lines to clearly see how it works.
And for example, a really simple thing, we shouldn't have to deal with lowercase and uppercase.if Q14.lower() == "c":
andstart.lower() == "yes":
will handle all forms of the response, saving lines.
-
muzan0jackson AuthorThank you very much, it finally works!!!!!!!!!!!
Thank you for your patience and your help!!!!!! : ) -
jee pee Moderatoryou once again failed to format the code, it must be because it is long.
the if on line 41 (and all the following ones) should be aligned with the if on line 30, it is misaligned.
--
a stranger is a friend we haven't met yet. -
muzan0jackson AuthorI followed your advice to the letter, but only the list gets mixed up, while the questions remain in the correct order. Moreover, when I make a mistake, it immediately shows me the score without having finished my 10-question quiz...
Sorry for the inconvenience : )-
-
muzan0jackson Author@jee pee
import random compteur = 0 start = input("Hello, are you ready to play?: ") if start == "yes": print ("Here we go!!!!") elif start == "Yes": print ("Here we go!!!!") else: print ("Goodbye...") Questions = [ "Q1", "Q2", "Q3", "Q4", "Q5", "Q6", "Q7", "Q8", "Q9", "Q10", "Q11", "Q12", "Q13", "Q14" ] Questions = random.sample(Questions,k = 10) print(Questions) for Q in Questions: if Q == "Q1": Q1 = input("What does NSI mean?: A-Digital Computer Sciences B-New Computer Sciences: ") if Q1 == "A": compteur += 1 print ("Well done!!!") elif Q1 == "a": compteur += 1 print ("Well done!!!") else: print ("Ouch, ouch, ouch...") if Q == "Q2": Q2 = input("What is the purpose of the int variable?: A-It allows the conversion to float B-It converts to Unicode C-It allows modifying a variable to an integer D-Nothing, it does not exist: ") if Q2 == "C": compteur += 1 print ("Well done!!!") elif Q2 == "c": compteur += 1 print ("Well done!!!") else: print ("Ouch, ouch, ouch...") if Q == "Q3": Q3 = input("What is the purpose of the str variable?: A-Nothing, it does not exist B-It allows the conversion to float C-It allows transforming most variables from another type into strings: ") if Q3 == "C": compteur += 1 print ("Well done!!!") elif Q3 == "c": compteur += 1 print ("Well done!!!") else: print ("Ouch, ouch, ouch...") if Q == "Q4": Q4 = input("What is the purpose of the float variable?: A-It allows the conversion to float B-It evaluates the content of its argument as if it were Python code C-Nothing, it does not exist: ") if Q4 == "C": compteur += 1 print ("Well done!!!") elif Q4 == "c": compteur += 1 print ("Well done!!!") else: print ("Ouch, ouch, ouch...") if Q == "Q5": Q5 = input("What is the decimal conversion of this binary number 0011?: A-3 B-4 C-1 D-11: ") if Q5 == "A": compteur += 1 print ("Well done!!!") elif Q5 == "a": compteur += 1 print ("Well done!!!") else: print ("Ouch, ouch, ouch...") if Q == "Q6": Q6 = input("What does F represent in hexadecimal?: A-16 B-11 C-10 D-15: ") if Q6 == "D": compteur += 1 print ("Well done!!!") elif Q6 == "d": compteur += 1 print ("Well done!!!") else: print ("Ouch, ouch, ouch...") if Q == "Q7": Q7 = input("What does D represent in hexadecimal?: A-16 B-13 C-10 D-15: ") if Q7 == "B": compteur += 1 print ("Well done!!!") elif Q7 == "b": compteur += 1 print ("Well done!!!") else: print ("Ouch, ouch, ouch...") if Q == "Q8": Q8 = input("Who invented the first algorithms?: A-Charles Babbage B-George Boole C-Al-Khwârizmi D-Al Mahani: ") if Q8 == "C": compteur += 1 print ("Well done!!!") elif Q8 == "c": compteur += 1 print ("Well done!!!") else: print ("Ouch, ouch, ouch...") if Q == "Q9": Q9 = input("What does A represent in hexadecimal?: A-10 B-11 C-9 D-12: ") if Q9 == "A": compteur += 1 print ("Well done!!!") elif Q9 == "a": compteur += 1 print ("Well done!!!") else: print ("Ouch, ouch, ouch...") if Q == "Q10": Q10 = input("What does C represent in hexadecimal?: A-12 B-10 C-14: ") if Q10 == "A": compteur += 1 print ("Well done!!!") elif Q10 == "a": compteur += 1 print ("Well done!!!") else: print ("Ouch, ouch, ouch...") if Q == "Q11": Q11 = input("What does E represent in hexadecimal?: A-11 B-10 C-14: ") if Q11 == "C": compteur += 1 print ("Well done!!!") elif Q11 == "c": compteur += 1 print ("Well done!!!") else: print ("Ouch, ouch, ouch...") if Q == "Q12": Q12 = input("What does B represent in hexadecimal?: A-12 B-10 C-14 D-11: ") if Q12 == "D": compteur += 1 print ("Well done!!!") elif Q12 == "d": compteur += 1 print ("Well done!!!") else: print ("Ouch, ouch, ouch...") if Q == "Q13": Q13 = input("What does CVS mean?: A-Comma Salsa Veranda B-Comma Separated Values C-Coca Sauce View: ") if Q13 == "B": compteur += 1 print ("Well done!!!") elif Q13 == "b": compteur += 1 print ("Well done!!!") else: print ("Ouch, ouch, ouch...") if Q == "Q14": Q14 = input("What does ASCII mean?: A-Armenian Standard Code for Information Interchange B-African Standard Code for Information Interchange C-American Standard Code for Information Interchange: ") if Q14 == "C": compteur += 1 print ("Well done!!!") elif Q14 == "c": compteur += 1 print ("Well done!!!") else: print ("Ouch, ouch, ouch...") print ("Your score is:", compteur,"/10") -
-
muzan0jackson Author@yg_beI seem to have done it, unless I'm mistaken.
-
jee pee Moderator@muzan0jacksonYou need to select all the code with the mouse and only then choose code+python, otherwise you miss the code selection and your Python ccm tags do not surround your code, but are placed before it.
-
-
jee pee ModeratorTo display the code correctly on the forum, you need to select the entire source first, and then select the code+python type. I've corrected your message (edit: your messages)
When I execute your code, I encounter an errorQuestions = [ Q1, Q2, Q3, Q4, Q5, Q6, Q7, Q8, Q9, Q10, Q11, Q12, Q13, Q14]
NameError: name 'Q1' is not defined
because the fields are not enclosed in " : "Q1" to be just text, not a variable.
With random, you could select 10 out of 14 questions, and then loop through these 10 questions with for.
Here's a part of the code to replace and extend over all the questions:
Questions = [ "Q1", "Q2", "Q3", "Q4", "Q5", "Q6", "Q7", "Q8", "Q9", "Q10", "Q11", "Q12", "Q13", "Q14" ] Questions=random.sample(Questions,k=10) print(Questions) for Q in Questions: if Q == "Q1": Q1 = input("What does NSI mean? : A-Digital Science Computing B-New Science Computing : ") if Q1 == "A": compteur += 1 print ("Well done!!!") elif Q1 == "a": compteur += 1 print ("Well done!!!") else : print ("Ouch, ouch, ouch...") if Q == "Q2": Q2 = input("What is the purpose of the int variable? : A-It allows conversion to float B-It converts to Unicode C-It allows modifying a variable to integer D-Nothing, it does not exist : ")
This is to stay in line with your source logic. So try to get it to work like this first.
But we can improve it, and surely reduce its size by 10 ;-))) We’ll see later
-
muzan0jackson AuthorYes, with while and for loops, but I've always had a hard time with that :)
-
-
-
muzan0jackson Author@yg_be
import random compteur = 0 nb_question = 0 nb_question += 1 start = input("Hello, are you ready to play?: ") if start == "yes": print ("Let's go!!!!") elif start == "Yes": print ("Let's go!!!!") else: print ("Goodbye...") Questions = ["Q1", "Q2", "Q3", "Q4", "Q5", "Q6", "Q7", "Q8", "Q9", "Q10", "Q11", "Q12", "Q13", "Q14"] random.shuffle(Questions) print(Questions) Q1 = input("What does NSI mean?: A-Digital Computer Science B-New Computer Science: ") nb_question += 1 if Q1 == "A": compteur += 1 print ("Well done!!!") elif Q1 == "a": compteur += 1 print ("Well done!!!") else: print ("Ouch, ouch, ouch...") Q2 = input("What is the purpose of the int variable?: A-It allows conversion to float B-It converts to Unicode C-It allows modifying a variable to an integer D-Nothing, it does not exist: ") nb_question += 1 if Q2 == "C": compteur += 1 print ("Well done!!!") elif Q2 == "c": compteur += 1 print ("Well done!!!") else: print ("Ouch, ouch, ouch...") Q3 = input("What is the purpose of the str variable? A-Nothing, it does not exist B-It allows conversion to float C-It allows converting most variables of another type to strings:") nb_question += 1 if Q3 == "C": compteur += 1 print ("Well done!!!") elif Q3 == "c": compteur += 1 print ("Well done!!!") else: print ("Ouch, ouch, ouch...") Q4 = input("What is the purpose of the float variable? A-It allows conversion to float B-It evaluates the content of its argument as if it were Python code C-Nothing, it does not exist: ") nb_question += 1 if Q4 == "C": compteur += 1 print ("Well done!!!") elif Q4 == "c": compteur += 1 print ("Well done!!!") else: print ("Ouch, ouch, ouch...") Q5 = input("What is the decimal conversion of this binary number 0011? A-3 B-4 C-1 D-11: ") nb_question += 1 if Q5 == "A": compteur += 1 print ("Well done!!!") elif Q5 == "a": compteur += 1 print ("Well done!!!") else: print ("Ouch, ouch, ouch...") Q6 = input("What does F represent in hexadecimal? A-16 B-11 C-10 D-15: ") nb_question += 1 if Q6 == "D": compteur += 1 print ("Well done!!!") elif Q6 == "d": compteur += 1 print ("Well done!!!") else: print ("Ouch, ouch, ouch...") Q7 = input("What does D represent in hexadecimal? A-16 B-13 C-10 D-15: ") nb_question += 1 if Q7 == "B": compteur += 1 print ("Well done!!!") elif Q7 == "b": compteur += 1 print ("Well done!!!") else: print ("Ouch, ouch, ouch...") Q8 = input("Who invented the first algorithms? A-Charles Babbage B-George Boole C-Al-Khwârizmi D-Al Mahani: ") nb_question += 1 if Q8 == "C": compteur += 1 print ("Well done!!!") elif Q8 == "c": compteur += 1 print ("Well done!!!") else: print ("Ouch, ouch, ouch...") Q9 = input("What does A represent in hexadecimal? A-10 B-11 C-9 D-12: ") nb_question += 1 if Q9 == "A": compteur += 1 print ("Well done!!!") elif Q9 == "a": compteur += 1 print ("Well done!!!") else: print ("Ouch, ouch, ouch...") Q10 = input("What does C represent in hexadecimal? A-12 B-10 C-14: ") nb_question += 1 if Q10 == "A": compteur += 1 print ("Well done!!!") elif Q10 == "a": compteur += 1 print ("Well done!!!") else: print ("Ouch, ouch, ouch...") Q11 = input("What does E represent in hexadecimal? A-11 B-10 C-14: ") nb_question += 1 if Q11 == "C": compteur += 1 print ("Well done!!!") elif Q11 == "c": compteur += 1 print ("Well done!!!") else: print ("Ouch, ouch, ouch...") Q12 = input("What does B represent in hexadecimal? A-12 B-10 C-14 D-11: ") nb_question += 1 if Q12 == "D": compteur += 1 print ("Well done!!!") elif Q12 == "d": compteur += 1 print ("Well done!!!") else: print ("Ouch, ouch, ouch...") Q13 = input("What does CVS mean? A-Comma Salsa Veranda B-Comma Separated Values C- Coca Sauce View: ") nb_question += 1 if Q13 == "B": compteur += 1 print ("Well done!!!") elif Q13 == "b": compteur += 1 print ("Well done!!!") else: print ("Ouch, ouch, ouch...") Q14 = input("What does ASCII mean? A-Armenian Standard Code for Information Interchange B-African Standard Code for Information Interchange C-American Standard Code for Information Interchange: ") nb_question += 1 if Q14 == "C": compteur += 1 print ("Well done!!!") elif Q14 == "c": compteur += 1 print ("Well done!!!") else: print ("Ouch, ouch, ouch...") print ("Your score is: ", compteur,"/10")
#Here is what I did, but I can't draw 10 questions at random.
-
-
yg_be ContributorHello, do you have an error message?
-
jee pee ModeratorHello,
For formatting sources, above the editing area of your messages, there are icons for text enhancement. The one that looks like<> ▼
allows you to apply the corresponding format to the code based on its type (C, Python, Shell, ...).
Here’s a little tutorial: https://codes-sources.commentcamarche.net/faq/11288-les-balises-de-code
Please send us your source in this format, not as images:def f(texte): codage={'a':111,'b':121,'c':211,'d':311,'e':151,'f':411,'g':113,'h':11,'i':21,'j':71,'k':19,'l':81,'m':100,'n':9,'o':1,'p':8,'q':1234,'r':1119,'s':4111,'t':5111,'u':555,'v':321,'w':0,'x':20,'y':30,'z':40,' ':951,"'":753} liste = [] for lettre in texte: valeur = codage.get(lettre) liste.append(valeur) return liste print(f('bonjour'))
-
muzan0jackson Author
import random compteur = 0 start = input("Hello, are you ready to play?: ") if start == "yes": print ("Let's go!!!!") elif start == "Yes" : print ("Let's go!!!!") else : print ("Goodbye...") Questions = [ Q1, Q2, Q3, Q4, Q5, Q6, Q7, Q8, Q9, Q10, Q11, Q12, Q13, Q14] random.shuffle(Questions) print(Questions) Q1 = input("What does NSI stand for?: A-Digital Computer Science B-New Computer Science: ") if Q1 == "A": compteur += 1 print ("Well done!!!") elif Q1 == "a": compteur += 1 print ("Well done!!!") else : print ("Ouch, ouch, ouch...") Q2 = input("What is the purpose of the int variable?: A-It allows conversion to float B-It converts to Unicode C-It allows modifying a variable to an integer D-Nothing, it does not exist: ") if Q2 == "C": compteur += 1 print ("Well done!!!") elif Q2 == "c": compteur += 1 print ("Well done!!!") else : print ("Ouch, ouch, ouch...") Q3 = input("What is the purpose of the str variable?: A-Nothing, it does not exist B-It allows conversion to float C-It allows transforming most variables from another type into strings: ") if Q3 == "C": compteur += 1 print ("Well done!!!") elif Q3 == "c": compteur += 1 print ("Well done!!!") else : print ("Ouch, ouch, ouch...") Q4 = input("What is the purpose of the float variable?: A-It allows conversion to float B-It evaluates the content of its argument as if it were Python code C-Nothing, it does not exist: ") if Q4 == "C": compteur += 1 print ("Well done!!!") elif Q4 == "c": compteur += 1 print ("Well done!!!") else : print ("Ouch, ouch, ouch...") Q5 = input("What is the decimal conversion of this binary number 0011?: A-3 B-4 C-1 D-11: ") if Q5 == "A": compteur += 1 print ("Well done!!!") elif Q5 == "a": compteur += 1 print ("Well done!!!") else : print ("Ouch, ouch, ouch...") Q6 = input("What does F represent in hexadecimal?: A-16 B-11 C-10 D-15: ") if Q6 == "D": compteur += 1 print ("Well done!!!") elif Q6 == "d": compteur += 1 print ("Well done!!!") else : print ("Ouch, ouch, ouch...") Q7 = input("What does D represent in hexadecimal?: A-16 B-13 C-10 D-15: ") if Q7 == "B": compteur += 1 print ("Well done!!!") elif Q7 == "b": compteur += 1 print ("Well done!!!") else : print ("Ouch, ouch, ouch...") Q8 = input("Who invented the first algorithms?: A-Charles Babbage B-George Boole C-Al-Khwârizmi D-Al Mahani: ") if Q8 == "C": compteur += 1 print ("Well done!!!") elif Q8 == "c": compteur += 1 print ("Well done!!!") else : print ("Ouch, ouch, ouch...") Q9 = input("What does A represent in hexadecimal?: A-10 B-11 C-9 D-12: ") if Q9 == "A": compteur += 1 print ("Well done!!!") elif Q9 == "a": compteur += 1 print ("Well done!!!") else : print ("Ouch, ouch, ouch...") Q10 = input("What does C represent in hexadecimal?: A-12 B-10 C-14: ") if Q10 == "A": compteur += 1 print ("Well done!!!") elif Q10 == "a": compteur += 1 print ("Well done!!!") else : print ("Ouch, ouch, ouch...") Q11 = input("What does E represent in hexadecimal?: A-11 B-10 C-14: ") if Q11 == "C": compteur += 1 print ("Well done!!!") elif Q11 == "c": compteur += 1 print ("Well done!!!") else : print ("Ouch, ouch, ouch...") Q12 = input("What does B represent in hexadecimal?: A-12 B-10 C-14 D-11: ") if Q12 == "D": compteur += 1 print ("Well done!!!") elif Q12 == "d": compteur += 1 print ("Well done!!!") else : print ("Ouch, ouch, ouch...") Q13 = input("What does CVS stand for?: A-Comma Salsa Veranda B-Comma Separated Values C-Coca Sauce View: ") if Q13 == "B": compteur += 1 print ("Well done!!!") elif Q13 == "b": compteur += 1 print ("Well done!!!") else : print ("Ouch, ouch, ouch...") Q14 = input("What does ASCII stand for?: A-Armenian Standard Code for Information Interchange B-African Standard Code for Information Interchange C-American Standard Code for Information Interchange: ") if Q14 == "C": compteur += 1 print ("Well done!!!") elif Q14 == "c": compteur += 1 print ("Well done!!!") else : print ("Ouch, ouch, ouch...") print ("Your score is:", compteur,"/10")
-