INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

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

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

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

(OP)
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

CODE --> 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() 

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

Copy error: Instead of * and / you write -, i.e.
print("What is the answer of", x1, "-", x2, "= ")

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

(OP)
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

CODE --> 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() 

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

What do you mean, with "divide does not work properly".
It's integer number division for example: 5 / 2 = 2

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

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 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close