SmallsJr
SmallsJr

Reputation: 1

I can't get the points in my game to add up

I am making a guessing game and I can't get the score to say anything but 0

I googled it and it came up with a formula that didn't work, so I came here.

Just in case the code looks weird, here's a link: http://www.codeskulptor.org/#user46_Rn6mbqc2fxzXniV.py

import random

totalScore = 0


lp = input ('Lets play the guessing game! \nPress Enter to start')
if (lp== ""):
    print ("Score\n_____")

x1 = int(input("No.1 Type a number between 0 and 5"))
if (x1 !=(random.randint(0,5))):
    input("Incorrect   :/\n Press Enter to move on")
    print ("+0")
    score = totalScore + 0
else:
    input("Correct!\n Press Enter to move on")
    print ("+1")
x2 = int(input("No.2 Type a number between 0 and 10"))
if (x2 !=(random.randint(0,10))):
    input("Incorrect   :/\n Press Enter to move on")
    print ("+0")
    score = totalScore + 0
else:
    input ("Correct!\n Press Enter to move on")
    print ("+1")
    score = totalScore + 1

x3 = int(input("No.1 Type a number between 0 and 15"))
if (x3 !=(random.randint(0,15))):
    input("Incorrect   :/\n Press Enter to move on")
    print ("+0")
    score = totalScore + 0
else:
    input ("Correct!\n Press Enter to move on")
    print ("+1")
    score = totalScore + 1

x4 = int(input("No.1 Type a number between 0 and 20"))
if (x4 !=(random.randint(0,20))):
    input("Incorrect   :/\n Press Enter to move on")
    print ("+0")
    score = totalScore + 0
else:
    input ("Correct!\n Press Enter to move on")
    print ("+1")
    score = totalScore + 1

x5 = int(input("No.1 Type a number between 0 and 25"))
if (x5 !=(random.randint(0,25))):
    input("Incorrect   :/\n Press Enter to move on")
    print ("+0")
    score = totalScore + 0
else:
    input ("Correct!\n Press Enter to move on")
    print ("+1")
    score = totalScore + 1

totalScore = score

print ("Total Score:")
print (totalScore)

I am trying to get the game to add all points earned and show them at the end.

Upvotes: 0

Views: 55

Answers (1)

CasualDemon
CasualDemon

Reputation: 6160

Each time you are setting score to 0, you are overwriting the previous score. What you want to do instead of score = totalScore + 1 (or 0) is simply totalScore += 1 (same as totalScore = totalScore + 1)

import random

totalScore = 0


lp = input ('Lets play the guessing game! \nPress Enter to start')
if (lp== ""):
    print ("Score\n_____")

x1 = int(input("No.1 Type a number between 0 and 5"))
if (x1 !=(random.randint(0,5))):
    input("Incorrect   :/\n Press Enter to move on")
    print ("+0")
else:
    input("Correct!\n Press Enter to move on")
    print ("+1")
    totalScore += 1
x2 = int(input("No.2 Type a number between 0 and 10"))
if (x2 !=(random.randint(0,10))):
    input("Incorrect   :/\n Press Enter to move on")
    print ("+0")
else:
    input ("Correct!\n Press Enter to move on")
    print ("+1")
    totalScore += 1

x3 = int(input("No.1 Type a number between 0 and 15"))
if (x3 !=(random.randint(0,15))):
    input("Incorrect   :/\n Press Enter to move on")
    print ("+0")
else:
    input ("Correct!\n Press Enter to move on")
    print ("+1")
    totalScore += 1

x4 = int(input("No.1 Type a number between 0 and 20"))
if (x4 !=(random.randint(0,20))):
    input("Incorrect   :/\n Press Enter to move on")
    print ("+0")
else:
    input ("Correct!\n Press Enter to move on")
    print ("+1")
    totalScore += 1

x5 = int(input("No.1 Type a number between 0 and 25"))
if (x5 !=(random.randint(0,25))):
    input("Incorrect   :/\n Press Enter to move on")
    print ("+0")
else:
    input ("Correct!\n Press Enter to move on")
    print ("+1")
    totalScore += 1

print ("Total Score:")
print (totalScore)

Example output:

Score
_____
+1
+0
+1
+0
+0
Total Score:
2

In the example above I also removed the superfluous score = totalScore + 0 as adding 0 to a integer does nothing.

Thanks for the codeskulptor link to make working with the code super easy!

Upvotes: 1

Related Questions