Как сделать мой код менее запутанным и более читабельным?Python

Программы на Python
Ответить
Anonymous
 Как сделать мой код менее запутанным и более читабельным?

Сообщение Anonymous »

Я начинаю работать с Python и хотел создать игру «Вопросы/ответы», но мне кажется, что это немного беспорядочно. Как я могу сделать его более читабельным и эффективным? больше всего я хочу изучить встроенные функции, поэтому, если вы сможете использовать встроенные функции, это поможет.
def new_game():#the function where all the game logic is

guesses=[]#the guesses the user had
correct_guesses=0
question_num=1
for quest in questions:#i specifically wanna shorten this part downward
print('---------------------------------------------------------')
print(quest)
for option in options[question_num-1]:
print(option)
guess=input("enter (A, B, C or D): ").upper()
guesses.append(guess)

correct_guesses+=check_answers(questions[quest], guess)#everytime it's correct it adds 1 and 0 if wrong
question_num+=1
display_score(correct_guesses, guesses)
#_________________________________________________________
def check_answers(answer, user_guess):
if answer == user_guess:
print("CORRECT")
return 1
else:
print("WRONG")
return 0

def display_score(correct_guess, guess):
print("-----------------------------------------")
print("RESULT:")
print("-----------------------------------------")
print("Answers: ", end=" ")
for i in questions:
print(questions.get(i), end=" ")#Can i do all of his in just 1 for loop??
print()
for j in guess:
print("Your guesses: ", end=" ")
print(" ".join(guess))
break

score=int((correct_guess/len(guess))*100)#percentage user got
print("score: ", score, "%")
def play_again():
play=input("do you wanna play again(y/n)??").lower()
if play == "y":
return True
else:
False

questions={"who created python? ": "B",
"From where is the name 'python' originating? ":"C",
"What year did the second ww? started": "A",
"What was UsainBolt time in 2009": "C"}

options=[['A.Bjarne stroute', 'B.Guido Von Rossum', 'C.Alexander Cobol', 'D.Blaise Pascal'],
['A.from the python snake', 'B.from his mother heart disease', 'C.From music band Monty python', 'D.None'],
["A.1939", "B.1969","C.1200'A.D'", 'D.2001'],
['A.9.2', 'B.7.67', 'C.9.58', 'D.10.05']]

new_game()

while play_again():#loop that restarts the code if play_again is true
new_game()

print("\nBye")


Подробнее здесь: https://stackoverflow.com/questions/798 ... e-readable
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Python»