Skip to content
Advertisement

How to stop the while loop from repeating in the output?

The while loop which asks the user for another number will NOT stop even though there is an false statement saying otherwise. In the output menu, the while loop keeps repeating. How do I make it move on to next section of my code.

import random #this allows randomiser functions such as generating both the player and computers number to work.

play = True
print("Hello! Welcome to 13 in - to Win")
name = input("What is your name? ")

while play == True:
  #print("The game starts here")
  AI_num = random.randint(1,19) #Generate a Random Number between 1 and 19 for the computer.
  Additional_Num = True
  PTotal = 0
  
  while Additional_Num == True:
    Player_Num = random.randint(1,10)
    PTotal = PTotal + Player_Num
    
    print(name,"Got A",Player_Num)
    print("Your Total is: ",PTotal) #Later add an input statement to ask user if they want to know their total
    
    play_again = input("Would You Like Another Number? <Yes or No>")  
      
  if play_again == "Yes":
         Additional_Num = True
  else:   
         Additional_Num = False
         print("AI got ",Additional_Num)
         print(name,"Got ",Player_Num)

    
  
  
  
  #print("13 in to Win Has Ended!")
 
  play_again = input("Would you like to play again? <Yes or No>")
  
  if play_again == "Yes":
    play = True
   
  else:   
     play = False
  print("Bye for now!")
  

Advertisement

Answer

You forgot to add an extra tab between the if/else statement.

Python relies on the indentation (whitespace at the beginning of a line) to define the scope in the code. this might be confusing while other programming languages often use curly brackets for this purpose.

if play_again == "Yes":
         Additional_Num = True
         
  else:   
         Additional_Num = False
         print("AI got ",Additional_Num)
         print(name,"Got ",Player_Num)
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement