python - While function issue keeps on spamming -
and made rock paper scissor game, when loose life or win, keep on spamming same message can help
import random #this allows me generate random numbers def main(): #all main code under print ("hello welcome rock, paper , scissors game!") #welcome message count = 0 #this counts how many tries tried while count != 3: #you can try 3 times print ("please choose whether:") #allows user choose between rock, paper , scissors print ("[1] rock") print ("[2] paper") print ("[3] scissors") user_choice = int(input()) #saves user input variable if user_choice == 1: user_choice = ("rock") elif user_choice == 2: user_choice = ("paper") elif user_choice == 3: user_choice = ("scissors") elif user_choice > 3: print ("invalid choice!") print ("restarting...") main() computer_choice = random.randint(1,3) #generates random number between 1 , 3 if computer_choice == 1: #define 1,2 , 3 computer_choice = ("rock") elif computer_choice == 2: computer_choice = ("paper") elif computer_choice == 3: computer_choice = ("scissors") def choice_1(): score = 0 lives = 2 if user_choice == "rock": if user_choice == computer_choice: print ("you chose:",user_choice) print ("--------vs--------") print ("computer chose:",computer_choice) print ("draw!") elif user_choice == "rock" , computer_choice == "paper": print ("you chose:",user_choice) print ("--------vs--------") print ("computer chose:",computer_choice) print ("you lost!") lives = lives - 1 elif user_choice == "rock" , computer_choice == "scissors": print ("you chose:",user_choice) print ("--------vs--------") print ("computer chose:",computer_choice) print ("you won!") score = score + 1 if user_choice == "paper": if user_choice == computer_choice: print ("you chose:",user_choice) print ("--------vs--------") print ("computer chose:",computer_choice) print ("draw!") elif user_choice == "paper" , computer_choice == "scissors": print ("you chose:",user_choice) print ("--------vs--------") print ("computer chose:",computer_choice) print ("you lost!") lives = lives - 1 elif user_choice == "paper" , computer_choice == "rock": print ("you chose:",user_choice) print ("--------vs--------") print ("computer chose:",computer_choice) print ("you won!") score = score + 1 if user_choice == "scissors": if user_choice == computer_choice: print ("you chose:",user_choice) print ("--------vs--------") print ("computer chose:",computer_choice) print ("draw!") elif user_choice == "scissors" , computer_choice == "rock": print ("you chose:",user_choice) print ("--------vs--------") print ("computer chose:",computer_choice) print ("you lost!") lives = lives - 1 elif user_choice == "scissors" , computer_choice == "paper": print ("you chose:",user_choice) print ("--------vs--------") print ("computer chose:",computer_choice) print ("you won!") score = score + 1 print ("you have",lives,"lives left") while lives != 0: choice_1() else: print ("oops! ran out of lives") print ("restarting...") main() count = count + 1 choice_1() else: print ("thanks playing!") if score == 3: print ("fantastic! scored:",score,"/3") elif score == 2: print ("good job! scored:",score,"/3") elif score == 1: print ("not bad! scored:",score,"/3") elif score == 0: print ("unlucky! scored:",score,"/3") main()
you should either break out of loop before calling main() function or restructure program in way more 1 instance of main() not running @ same time. 1 of dangers of calling function inside of itself. there other threads discuss recursive function calling if want read them.
Comments
Post a Comment