Python Project: Millionaire

python key on colored keyboard

Home > python > python project millionaire

We will develop this page in the future but for now we will publish the code for you.

LESSON 1

To watch a video tutorial on how to start the ‘who wants to be a millionaire project’, and what we will learn in python during this project video the starter video here: Millionaire Project – How to Start – Python Tutorial  for Starters

To watch a video tutorial on how to check for the correct answer using if statements including if-else and efif in the project ‘who wants to be a millionaire project’, watch the video here: Millionaire Project – If- Python Tutorial  for Starters

If Statement

choice = 'a' 
if choice == 'a':
	print ('you have chosen a')

If-Else Statement

choice = 'a' 
if choice == 'a':
	print ('you have chosen a')
else:
	print ('you have not chosen a')

If-Elif-Else Statement

number = -2
if number == 0:
	print ('number is 0')
elif number > 0:
	print ('positive number')
else:
	print ('negative number')

Multiple If-Elif-Else Statement

choice = 'a' 
if choice == 'a':
	print ('you have chosen a')
elif choice == 'b':
	print ('you have chosen b')
elif choice == 'c':
	print ('you have chosen c')
elif choice == 'd':
	print ('you have chosen d')
else:
	print ('you need to enter a, b, c or d')
international students learning python

Print in Python

Learn about how to use print in python.

This article is the ultimate guide to using the print function in python

LESSON 2

We don’t know what you will do first in lesson 2 so we want to cover all concepts that you might use in both lesson 2 and 3 just in case you need them. So what does this include:

  1. data types
  2. for loops
  3. range
  4. while loops
  5. try and except

We will try to add the link s to each of the videos in the current series and also some longer (older) videos if you wish to know more about the concept. Here is the code from those videos to help you with the project.

Data Types

The video for data types used in the python project millionaire is at https://www.youtube.com/watch?v=r7Yf1aPdwtM

choice = input("enter your choice 1, 2, 3 or 4: ")
print(choice)
print(choice*3)
print(type(choice))

choice = float(choice)
print(choice)
print(choice*3)
print(type(choice))

For Loops

The video for the for loops used in the python project millionaire is at https://www.youtube.com/watch?v=R9XmKMiptj4

for i in range(5):
	print("play game")
for num in [2, 4, 6]:
	print(num)
name = "Peak"
for letter in name:
	print("Hello", letter)
questions = ['q1', 'q2', 'q3']
for q in questions:
	print(q)
count = 1
questions = ['q1', 'q2', 'q3']
for q in questions:
	print(count,q)
	count = count + q1
print('end')
name = "Wachi Gineer"
for i in name:
    print (i)
print("end")
questions = ['q1','q2','q3','q4','q5']
for q in range(len(questions)):
    print(questions[q])

Range

The video for the range() function used in the python project millionaire is at https://www.youtube.com/watch?v=HCkC8D28ev0, good luck!

for loopcount in range(3):
   print (loopcount,"question")
print("end)")
for loopcount in range(1,4):
   print (loopcount,"question")
print("end)")
for loopcount in range(2,11,2):
   print (loopcount,"question")
print("end)")
for q in range(15):
   print ("question",q)
for q in range(1,16):
   print ("question",q)

While Loops

The video for the while loops used in the python project millionaire is at https://www.youtube.com/watch?v=s-XJO7KELqY

number = 5
while number > 0:
    print(number)
    number = number + 1
print("end")
count = 1
while count <= 0:
    print(count, "hello")
    count = count + 1
print("end")
count = 1
while True:
    print(count, "hello")
    if count == 10:
        break
    count = count + 1
print("end")
while True:
   choice = input("enter your choice: ")
   if choice == 'a':
       print ('you have chosen a')
       break
   elif choice == 'b':
       print ('you have chosen b')
       break
   elif choice == 'c':
       print ('you have chosen c')
       break
   elif choice == 'd':
       print ('you have chosen d')
       break
   else:
       print ('you need to enter a, b, c or d')
correct_answer = "b"

while True:
    try:
        choice = input("enter a, b, c or d")
        if choice in ['a','b','c','d']:        
            if choice == 'b':
                print ('correct answer')
                break
            else:
                print ('sorry wrong answer') 
                break
    except:
        print("there seems to be a problem")
        continue

Try and Except

The video for the while loop and try and except used in the python project millionaire is at https://www.youtube.com/watch?v=HhFUeRNNkZ0

while True:
    try:
        option = input('enter 1,2,3,4 or x to exit')
        if option == 'x':
            break
        else:
            option = int(option)
            if option > 0 and option < 5:
                print(option)
                continue
            else:
                print ('enter 1, 2, 3, 4, or x to exit')
    except:          
        print ('enter a number - 1, 2, 3, 4, or x to exit') 

LESSON 3

We hold data in variables such as numbers and text but we also need to be able to hold multiple items sometimes. For example, if we need to know the ingredients for a recipe, or list items we need to buy, we use a (shopping) list.

In python we use data structures, and the most popular is called the list. It is a collection of items separated by commas inside square brackets.

In our coding of the millionare game, how we store and access the questions, options and answers is very important. We would ultimately like to keep different sets in text files, and we will provide the code to do this soon. But even if all the questions, options and answers are stored in a text file, they need to be brought into the python program.

In summary, lists are a good way to hold and access our data for the millionaire game. How we hold the data is dependent on our design.

So here are two video to help:

  1. Understand python lists
  2. Your lists in millionaire – what so we need to think about?

Lists

mylist = ['first question', 'first question', 'first question']
for q in mylist:
	print(q)
#          0   1  2   3  4
mylist = [10, 20,'c',40,'5']
print(mylist[1])
mylist = [10, 20,'c',40,'5']
for i in range(5):
	print(i)
mylist = [10, 20,'c',40,'5']
for i in range(5):
	print(i,mylist[i])

Learn by Success through Failure or Waste Time?

When you design a graph you can use the x-axis or the y-axis for the two variables, but which is x and which is y? This is up to you. You can try it and see what is best.

In python, after you start to master the basics and understand the concepts, you will need to learn programming logic and how to design your program.

A common approach is to write in comments what you plan to do (order), and then write functions to achieve these sub tasks. We will show this soon. But, at present, our first design issue is how are we going to store data in the form of the questions, options and answers.

Normally we can look online or AI and use them as a guide, but are they correct for ‘your’ design? Maybe not!

It is a valuable lesson to look at the second video on lists to start to use your own thoughts to why you are doing what you are doing. Be careful, when you take the wrong road you will not get to your preferred destination, have to backtrack to where you made the mistake, and suffer frustration because of this mistake.

If you understand why you have done something and it is wrong then it is valuable learning, if you just copy other code without understanding then you just waste a lot of time.

optionsA = ['a1','a2','a3','a4']
optionsB = ['b1','b2','b3','b4']
optionsC = ['c1','c2','c3','c4']
optionsD = ['d1','d2','d3','d4']

for i in range(15):
    print(i+1,optionsA[i],optionsB[i],optionsC[i],optionsD[i])

LESSON 4

Lists Inside a List

There is another approach to storing the quesitons and options and that is to use lists inside a list. These are called nested lists, and you can use a nested loop to access the nested list elements. Here is the code:

questions = [
'Q1: What is 100/20',
'Q2: What is 16/4',
'Q3: What is 12/2',
]

solutions = [
['A. 3','B. 4','C. 5','D. 6'],
['A. 3','B. 4','C. 5','D. 6'],
['A. 3','B. 4','C. 5','D. 6']
]

for i in range(3):
    print(questions[i])
    for j in range(4):
        print(solutions[i][j])

For a complete explanation of this code and how it work please watch the python tutorial list in list video.

Functions

Functions are a very important part of programming so we encourage you to learn them. 

The main stages of learning about functions are as follows:

  • define a function
  • call a function
  • pass to a function
  • return from a function

Every stage is explained clearly in the python function tutorial video.

Define a Function

def simple():
    print("simple")   

Call a Function

def simple():
    print("simple")   
    
simple()
simple()
simple()

Pass to a Function

def fav_number(number):
    print("my favourite number is", number)

number = 7
fav_number(number)
def fav_number(n1,n2):
    print("my favourite number is", n1)
    print("your favourite number is", n2)


number = 7
num = 3
fav_number(number,num)

Return from a Function

def fav_number(n1,n2):
    print("my favourite number is", n1)
    print("your favourite number is", n2)
    total = n1+n2
    return total



number = 7  
num = 3
total = fav_number(number,num)
print(total)   
def fav_number(n1,n2):
    print("my favourite number is", n1)
    print("your favourite number is", n2)
    total = n1+n2
    return total



number = "hello"   
num = " world"
total = fav_number(number,num)
print(total)   

Useful Code

These two lines of code are useful to 

  1. make a copy of a list
  2. to sort the list items into order (e.g. 1,2,3, … or ‘a’, ‘b’, ‘c’, …)
copy_list = mylist[:]
copy_list.sort() 

This line of code achieves two things: it removes the last element in a list by using .pop(), and, if the list is in order, it takes the value of the highest item in the list which is being popped from the list.

highest = copy_list.pop()

Although you are unlikely to need to use the index function, here is the correct syntax to find the index, the location, of an item in a list. This function looks for a value front he start of the list. When it finds that value it will return the location of that item, the index number.

So, for example, if we are looking for the value 10 in a list that has this value in the first and last positions, this index function will return the location of the first instance, which will be the index value of 0.

In this specific example we are looking for the highest value in a list which has the value held in the highest variable. If and when it finds this value in the list called mylist, then it will return the index number into the variable find_top.

find_top = mylist.index(highest)