Click here to Skip to main content
15,899,314 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Have looked around but cant seem to find any help. Basically my problem is at the bottom of the code with the line `score = score * 10'
it returns score as 0 when I print it in the next line. I want to multiply it by 10 as the score out of ten needs to be converted to a percentage. If you have any other ideas on how to do this please suggest them.

Python
#This is where the import commands go
    from random import randint
    import random
     
    #This is a set list of variables for each question (21 in total as 2 for each question and operation)
    
    operations = ['x', '-', '+']
    operation = random.choice(operations)
    number1 = random.randrange(0,10)
    number2 = random.randrange(0,10)
    
    
    #This variable stores the users score and the correct answer for each question
    score = 0
    answer = None
    numberofquestions = 0
    
    
    
    """ THIS IS OLD CODE ********** IGNORE OLD CODE***********
    #This changes the randomly generated numbers into strings, then evauates them as maths equations so it knows the answers.
    #Also it stores the whole of each question as a variable, making it easier to code.
    question1 = eval(str(q1p1) + operation + str(q1p2))
    THIS IS OLD CODE ********** IGNORE OLD CODE***********"""
    
    
    
    #This asks for the user’s name
    
    name = input("What is your forename (With No Caps?)")
    surname = input("What is your surname (With No Caps?)")
    
    
    #This prints a welcome message, personalised with the user's name
    
    print("Welcome to your test," + name)
    
    
    #Information about how to answer questions
    
    print('''Throughout your test you will be asked a series of questions.
    These questions will be randomly generated and are designed to test your basic arithmetic skills.
    Please enter your answers in integer form.
    EG. if the question was 5 + 5, you would enter 10 with no spaces or extra characters.''')
    
    
    
    #First question
    while numberofquestions <10:
    	operations = ['x', '-', '+']
    	operation = random.choice(operations)
    	number1 = random.randrange(0,10)
    	number2 = random.randrange(0,10)
    	if operation == '+':
    		answer = number1 + number2
    	elif operation == '-':
    		answer = number1 - number2
    	elif operation == 'x':
    		answer = number1 * number2
    	user_answer = input("What is " + str(number1) + " " + operation + " " + str(number2) + "?")
    	user_answer = float(user_answer)
    	if user_answer == answer:
    		print("Well Done! You got it correct!")
    		score = score+1
    	else:
    		print("Sorry you got that wrong")
    	print ("***********-*-*-*-Your score so far is-*-*-*-*********** ")
    	print (score)
    	numberofquestions = numberofquestions+1
    print ("Well done, you have completed your test! Your final score was...")
    print (score)
    score = score * 10
    
    print ("Your score as a percentage is," +str(score))


What I have tried:

i have tried playing about with different ways of coding the percentage variable (score) and have just sat blankly for 10 minutes
Posted
Updated 20-May-16 10:33am
Comments
Richard MacCutchan 20-May-16 8:18am    
I just tried that with an initial value of 7 and it printed it correctly as 70.
Richard MacCutchan 20-May-16 8:27am    
Just tried all your code and it correctly calculates and prints the percentage. Have you checked the indentation to ensure that the last four statements are not considered part of the while loop?

1 solution

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

Use the debugger to ensure your code is understood correctly by python.
put a breakpoint just before you calc the percentage and check everything is correct.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900