0
How can I maintain my score if I continue... please help to clear this....
import random def again(): score=0 print("score",score) b=random.randint(1,10) print("Guess The Numbers//1 to 10 //",b) a= int (input("Enter Guess Number :")) if b==a: print("You Win ! The Guess No. Is" ,b) print("score",score+1) else: print("You Loose. The Guess No.Is\n" ,b) again() again()
1 Odpowiedź
+ 1
You can extend this if you want to:
import random as rnd
class GuessNumber:
def __init__(self, player, rndMax):
self.player = player
self.score = 0
self.threshold = rndMax
self.correct = None
def Guess(self, n):
c = rnd.randint(0, self.threshold)
self.correct = c
if c == n:
self.score += 1
return True
return False
def Score(self):
return self.score
def CorrectAnswer(self):
return self.correct
def NewGame(self):
self.score = 0
self.correct = None
# Demo: input player name
player = input("Player name: ")
print("{}\nWelcome {}\n".format(player, player))
o = GuessNumber(player, 10)
x, guess = 0, 0
while x < 20: # simulate 20 times
guess = rnd.randint(0, 10)
print("Guessing ...", guess)
if o.Guess(guess):
print("Right! you scored:", o.Score())
else:
print("Sorry, correct answer", o.CorrectAnswer())
x += 1
print("\n* {} scored: {}".format(o.player, o.score))