Python Forum
Multiple Question Results code - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Multiple Question Results code (/thread-2399.html)



Multiple Question Results code - flynnmackie - Mar-13-2017

Hi, i recently began looking at python, and therefore my code is very basic, however i am looking at making a (very basic) game, just to test myself on coding, so far i have the code:
---

def menu():
    print("Question 1 of 10, \n 1. Start Game \n 2. Cheats")
    choice = input()

    if choice == "1":
        time.sleep(0.7)
        print("Q1")

    if choice == "2":
        time.sleep(0.7)
        print("Q2")

    if choice == "3":
        time.sleep(0.7)
        print("Q3")
menu()
---
This code is repeated ten times, and for each answer i want a definitive value, such as answering Q1 would deposit say, 3 points (I haven't decided yet) in a 'bank' of sorts. Then when all the questions are finished the amount of points would determine a player for the player to use, e.g less then 30 wold assign value x, less then 50 would assign value y, and over 50 would assign value z.

Is there anyway this code could be used? Or is this too advanced,
Thank You Tongue


RE: Multiple Question Results code - zivoni - Mar-13-2017

I am not entirely sure what do you want, but you might consider to use a dictionary to store your questions/point values - that way you would not need to repeat basically same code ten times.

questions = {"1": "Text of question 1", "2": "Text of question 2", "3": "Text of question 3"}

choice = input()

time.sleep(0.7)
print(questions.get(choice, "Choice must be 1..3"))
you can use questions[choice] to get the value from a dictionary, but with .get() you wont get error when your choice is not in the dictionary.


RE: Multiple Question Results code - snippsat - Mar-13-2017

As mention bye zivoni a dictionary is one way do it.
To take it a step further that do counting.
import random

def quiz_dict():
   return {
     "How many days ion a week? ": '7',
     "Is linux a OS? ": 'yes',
     }

def quiz(quiz_dict):
   score = 0
   keys = quiz_dict.keys()
   for quiz_numb, question in enumerate(keys, 1):
       answer = input('{}. {}'.format(quiz_numb, question))
       if quiz_dict[question] == answer:
           print('Correct')
           score += 1
       else:
           print('Not correct')
   print('Total score for this round {}'.format(score))

if __name__ == '__main__':
   quiz(quiz_dict())
You can also look at this post where a talk a little about one way to do a menu system.