Python Forum
Multiple Question Results code
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Multiple Question Results code
#1
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
Reply
#2
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.
Reply
#3
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Code works but doesn't give the right results colin_dent 2 728 Jun-22-2023, 06:04 PM
Last Post: jefsummers
  python multiple try except block in my code -- can we shorten code mg24 10 6,177 Nov-10-2022, 12:48 PM
Last Post: DeaD_EyE
  Compiling Python 3.8.5 source code results in build error Deepan 0 2,194 Sep-14-2020, 04:11 AM
Last Post: Deepan
  Search Results Web results Printing the number of days in a given month and year afefDXCTN 1 2,245 Aug-21-2020, 12:20 PM
Last Post: DeaD_EyE
  question: finding multiple strings within string djf123 4 3,001 May-16-2020, 01:00 PM
Last Post: snippsat
  Different results of code with local account and technical account dreyz64 7 3,702 Mar-05-2020, 11:50 AM
Last Post: dreyz64
  How to append one function1 results to function2 results SriRajesh 5 3,177 Jan-02-2020, 12:11 PM
Last Post: Killertjuh
  multiple problems with code SrijaRamarthy 2 1,869 Nov-06-2019, 06:24 AM
Last Post: SrijaRamarthy
  printing multiple results together kumaaaa 3 1,830 Sep-29-2019, 02:19 AM
Last Post: kumaaaa
  How to execute a string with multiple sqlite statements and print results? shanepy 0 2,269 Nov-20-2018, 10:19 PM
Last Post: shanepy

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020