My problem in my program is that it does not retain the score of the computer and the player. I know that my problem is that the loop will declare it again as 0. Is there any solution for this? I am just learning python at my university so I cannot process it deeply.
import random
print("WELCOME TO THE GUESSING GAME!")
name = input("Please enter your name: ")
def main():
first = int(input("Enter a starting number: "))
second = int(input("Enter an ending number: "))
chances = 3
x = random.randint(first, second)
print("You've only ",
chances,
" chances to guess the secret number.\n")
tries = 0
player = 0
computer= 0
while tries < chances:
tries += 1
guess = int(input("Guess a number: "))
if x > guess:
print("Sorry " + name + " you guessed too small.")
if x < guess:
print("Sorry " + name + " you guessed too high.")
if x == guess:
player += 1
print("The player has" , player, "points")
print("Congratulations " + name + " you guessed it")
if computer == 3:
print("The Computer won. Better luck next time,", name)
break
if player == 3:
print("The Player won! Congrats", name)
break
else:
computer += 1
print("The computer has" , computer, "points")
print("\nThe secret number is", x)
print("\t\nGAME OVER!")
restart=input("Ready for the next round? Type yes or no:").lower()
if restart == "yes":
print("\n\tGood luck" , name)
main()
else:
print("\n\tThank you for playing", name)
exit()
main()
Aucun commentaire:
Enregistrer un commentaire