Greg Peckory - 1 year ago 131
Python Question

# how to add score to higher or lower game

``````from random import *

while True:

random1 = randint(1,20)

random2 = randint(1,20)

print("h = higher, l = lower, s = same, q = quit")

print(random1)

a = input()

if a.lower() == 'q':
break

print(random2)

if a.lower() == 'h' and random1 < random2:

print("Well done")

elif a.lower() == 'l' and random1 > random2:

print("Well done")

elif a.lower() == 's' and random1 == random2:

print("Well done")
else:

print("Loser")
``````

So what I am trying to do is have x as my score. And when the answer prints "Well Done" I would like it to add 10 points to my score and then print the score. The thing is the score seems to reset loads of times throughout the game and I would like it to either add 10 to the score or stay the same. Does anyone know a way of doing this in my program. I can't imagine it would be too hard but I am only a beginner and still learning. At the moment I have no score at all added to my program, just so you can show me the easiest/ best way too approach this. Thanks for the help :)

``````x = 0 # initialize the score to zero before the main loop
while True:

...

elif a.lower() == 's' and random1 == random2:
x += 10 # increment the score
print("Well done. Your current score is {0} points".format(x))
``````

anyway, the whole code could be shortened to:

``````from random import *
x = 0
while True:
random1 = randint(1,20)
random2 = randint(1,20)
print("h = higher, l = lower, s = same, q = quit")
print(random1)
a = input().lower()
if a == 'q':
break

print(random2)

if ((a == 'h' and random1 < random2) or
(a == 'l' and random1 > random2) or
(a == 's' and random1 == random2)):
x += 10
print("Well done. Your current score is: {0}".format(x))
else:
print("Loser")
``````
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download