Я запускаю 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
Можете ли вы помочь мне сделать мой код менее запутанным и более читабельным? ⇐ Python
Программы на Python
1772385403
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")
Подробнее здесь: [url]https://stackoverflow.com/questions/79898958/can-you-help-me-make-my-code-less-messy-and-more-readable[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия