Ask coding questions

← Back to all posts
Please can someone help me improve this code.
synergyedge (1)

import time
import sys
abc = "Welcome to my program, please enjoy :)\n"
for l in abc:
sys.stdout.write(l)
sys.stdout.flush()
time.sleep(0.2)
print()
username1 = input("Please enter a username which is under 10 characters long: ")
if len(username1) < 10:
print("You are now known as", username1)
else:
username1 = input("Please enter a shorter username: ")
print()
password = input("Enter a password under 6 characters: ")
print()
if len(password) <6:
print("Welcome to the games")
else:
password = input("You do not have access to this game till you use a password that meets the requirements: ")
print()

#----------------------------------------------------#

question1 = input("A song called 'Fine Girl' was sung by which person...\n A.Big Sean\n B.James Charles\n C.ZieZie\n").upper()
if question1 == "C":
print("Correct, proceed to next question. You have been awarded 2 point")
else:
question1redo = str(input("Incorrect... try again: ")).upper()
if question1redo == "C":
print("Correct, proceed to next question. You have been awarded 1 point")
print()

#----------------------------------------------------#

question2 = input("Which of these 2 songs have more views on YouTube:\n A.Rap God by Eminem\n B.Swevin by A Boogie wit da Hoodie\n").upper()
if question2 == "A":
print("Correct, eminems rap god got 887 million views\n")
else:
question2redo = str(input("Incorrect, choose again... ")).upper()
if question2redo == "A":
print("Proceed to the next question")
else:
print("You have failed")
print()

#-----------------------------------------------------#

question3 = str(input("How many subscribers does the most subscribed youtube channel have? \n A.10 Million\n B.49.5 Million\n C.215.1 Million\n D.100 Million\n")).upper()
if question3 == "D":
print("Correct, move onto the next question...\n")
else:
question3redo = str(input("Try again... "))
if question3redo == "D":
print("Correct, move onto the next question...\n")
else:
print("incorrect, you have failed...")

Commentshotnewtop
malvoliothegood (107)

1) The code that you use for the asking of questions is repetitive so you can put it in a function. Name could be something like ask_question

2) You can put the questions, possible answers and the correct answer in a data structure such as this one:

questions = [
["A song called 'Fine Girl' was sung by which person...", " A. Big Sean\n B. James Charles\n C. ZieZie\n", "C"]
["Which of these 2 songs have more views on YouTube", "A.Rap God by Eminem\n B.Swevin by A Boogie wit da Hoodie\n", "A"]
# and so on
]
# Use a while loop to loop through the questions list, get each question and pass it to the question asking function.
counter = 0
while (counter < len(questions)):
    ask_question(questions[counter])
    # More code here...
    
    counter += 1
ryanhcode (85)

Can you give us a link to the repl and tell us what you want improved?