Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

brand new to python, program works but on testing has some problems 1

Status
Not open for further replies.

69ashton

Programmer
May 22, 2016
11
GB
I am new to python coding.

I am writing a program, a quiz that asks mathematical questions, it will ask 3 maths question and
randomly choose one of 4 operators, +,-,*,/
if the quizzer gets the question right the score will go up by one

I have tested this code many times but it has never done * or divide, it only does + and -
also sometimes the scoring works fine and other times the scoring does not work at all

Python:
import random

#Defining Addition, Subtraction, Multiplication, Division
def add(x1, x2):
   return x1 + x2
def sub(x1, x2):
   return x1 - x2
def mul(x1, x2):
   return x1 * x2
def div(x1, x2):
   return x1 / x2
#this function will return the users answer for the mathematical quiz question
def check(x1,x2):
    answer = int(input("What is the value"))
    return answer

def quiz():
#this will keep a count of how many times the question was asked,
#the quiz will end when 3 questions have been asked
    count = 0;
#this will keep a score of how many questions the player got right,
    score =0;
    
    while (count !=3):
        value = random.randint(0,3)
        if value == 0:
                        x1 = int(input('First number?\n'))
                        x2 = int(input('Second number?\n'))
                        print("What is the answer of", x1, "+", x2, "= ")
                        if add(x1,x2) == check(x1,x2):
                            score +=1
                        count +=1
        elif value == 1:
                        x1 = int(input('First number?\n'))
                        x2 = int(input('Second number?\n'))
                        print("What is the answer of", x1, "-", x2, "= ")
                        if sub(x1, x2) == check(x1,x2):
                            score +=1
                        count +=1
        elif value == 2:
                        x1 = int(input('First number?\n'))
                        x2 = int(input('Second number?\n'))
                        print("What is the answer of", x1, "-", x2, "= ")
                        if mul(x1, x2) == check(x1,x2):
                            score +=1
                        count +=1
        elif value == 3:
                        x1 = int(input('First number?\n'))
                        x2 = int(input('Second number?\n'))
                        print("What is the answer of", x1, "-", x2, "= ")
                        if div(x1, x2) == check(x1,x2):
                            score +=1
                        count +=1
    if count == 3:
        print("You have scored ", score, "out of 3")
        exit()
      
quiz()
 
Copy error: Instead of * and / you write -, i.e.
print("What is the answer of", x1, "-", x2, "= ")
 
Thank you i have changed the code as mikrom has suggested
however the divide does not work properly, how can i change the code so it will work to accept a division value
Python:
import random

#Defining Addition, Subtraction, Multiplication, Division
def add(x1, x2):
   return x1 + x2
def sub(x1, x2):
   return x1 - x2
def mul(x1, x2):
   return x1 * x2
def div(x1, x2):
   return x1 / x2
#this function will return the users answer for the mathematical quiz question
def check(x1,x2):
    answer = int(input("What is the value"))
    return answer

def quiz():
#this will keep a count of how many times the question was asked,
#the quiz will end when 3 questions have been asked
    count = 0;
#this will keep a score of how many questions the player got right,
    score =0;
    
    while (count !=3):
        value = random.randint(0,3)
        if value == 0:
                        x1 = int(input('First number?\n'))
                        x2 = int(input('Second number?\n'))
                        print("What is the answer of", x1, "+", x2, "= ")
                        if add(x1,x2) == check(x1,x2):
                            score +=1
                        count +=1
        elif value == 1:
                        x1 = int(input('First number?\n'))
                        x2 = int(input('Second number?\n'))
                        print("What is the answer of", x1, "-", x2, "= ")
                        if sub(x1, x2) == check(x1,x2):
                            score +=1
                        count +=1
        elif value == 2:
                        x1 = int(input('First number?\n'))
                        x2 = int(input('Second number?\n'))
                        print("What is the answer of", x1, "*", x2, "= ")
                        if mul(x1, x2) == check(x1,x2):
                            score +=1
                        count +=1
        elif value == 3:
                        x1 = int(input('First number?\n'))
                        x2 = int(input('Second number?\n'))
                        print("What is the answer of", x1, "/", x2, "= ")
                        if div(x1, x2) == check(x1,x2):
                            score +=1
                        count +=1
    if count == 3:
        print("You have scored ", score, "out of 3")
        exit()
      
quiz()
 
What do you mean, with "divide does not work properly".
It's integer number division for example: 5 / 2 = 2

 
If you want to have real number division, e.g. 5 /2 = 2.5 then you have change two things:

1. Function div() should return real number
Code:
def div(x1, x2):
   #return x1 / x2
   return float(x1) / float(x2)

2. function answer() should not convert everything to integer, i.e.:
Code:
def check(x1,x2):
    #answer = int(input("What is the value"))
    answer = input("What is the value ")
    return answer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top